首页 > 解决方案 > 为什么我会收到 405 错误?

问题描述

我对网络编程很陌生,我正在尝试建立一个带有服务器端的小网站。这是代码:

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!";
?>

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

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

我必须补充一点,我在没有管理员权限的情况下运行整个事情。但是,由于网络服务器似乎以某种方式运行,因为我可以加载网站,我想知道这是否可能是由于机器上的低权限以外的其他原因。

标签: javascriptphpjqueryhtmlnode.js

解决方案


推荐阅读