javascript エラー「Failed to load resource: the server responded with a status of 405 ()」が発生した場合の対処法

javascript エラー「Failed to load resource: the server responded with a status of 405 ()」が発生した場合の対処法

javascriptで、エラー「Failed to load resource: the server responded with a status of 405 ()」が発生した場合の原因と解決方法を記述してます。

環境

  • OS windows11 pro 64bit
  • webサーバー node製のhttp-server
  • ブラウザ chrome 103.0.5060.134

エラー内容

以下の、「submit」ボタンをクリック時に発生。

<form id="frm" action="#" method="post">
    <input type="submit" name="sb" value="送信">
</form>

<script>

document.getElementById('frm').onsubmit = function(e) { 
    console.log(e)
};

</script>

エラーメッセージ

Failed to load resource: the server responded with a status of 405 ()

画像

firefox102の場合は、以下のエラーが発生します。

POSThttp://mebee.com:8080/sample.html#
[HTTP/1.1 405 Method Not Allowed 4ms]

画像

原因

「http-server」だと「post」が使用できないため

解決方法

「post」が使用できる「apache」などのwebサーバーを使用する