为什么会出现405错误?

时间:2018-07-25 07:37:15

标签: javascript php jquery html node.js

我是Web编程的新手,我正在尝试建立一个带有服务器端的小型网站。 这是代码:

class userData {
  catchDropDownSelection() {
    this.dropDownSelection = $('#xDropDown').prop('selectedIndex');
  }
}

class sendData {
  constructor(userData) {
    this.userDataClone = userData;
  }

  sendDropDownSelection() {
    //this.userDataClone.catchDropDownSelection();
    $.post("MyFirstPhP.php", {
      userSelection: "dummy" //this.userDataClone.dropDownSelection
    }, function(data) {
      //this.testFunction()
      $('#testOutput').html("data");
      //document.getElementById("testOutput").innerHTML = "data"
    }).fail(function() {
      console.log("$.post failed!");
    })
  }

  testFunction() {
    //document.getElementById("testOutput").innerHTML = "data"
    $('#testOutput').html("data");
  }
}

var userDataInstance = new userData;
var sendDataInstance = new sendData(userDataInstance);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>

<head>
  <title>PHP is Awesome!</title>
  <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
  <link rel="stylesheet" type="text/css" href="MyFirstPhP.css">
</head>

<body>
  <select id="xDropDown">
    <option value="test1">test1</option>
    <option value="test2">test2</option>
  </select>
  <button onclick="sendDataInstance.sendDropDownSelection()">ClickMe!</button>
  <p id="testOutput">not yet</p>
  <script src="MyFirstPhP.js"></script>
</body>

</html>

php代码在这里:

<?php
  session_start();
  $dropDownSelection = $_POST['userSelection'];
  echo $dropDownSelection
  //$dropDownSelection = $dropDownSelection . "wurde verarbeitet!";
?>

我正在本地主机(端口8080)上使用node.js运行服务器。到目前为止,它可以正常工作,启动服务器后,我可以加载页面并与其进行交互。 但是,当我按下按钮,触发对php的AJAX调用时,在控制台日志中出现以下错误:

  

jquery-3.3.1.min.js:2 POST http://localhost:8080/MyFirstPhP.php 405(不允许使用方法)

我必须补充一点,我正在运行整个过程而没有管理员权限。但是,由于Web服务器似乎可以在我加载网站时运行,所以我想知道这是否是由于其他原因而不是计算机的特权较低。

0 个答案:

没有答案