XMLHttpRequest() POST 返回 405 错误;不允许的方法

5 html javascript php xampp http-status-code-405

我的html代码:

    <h2>Please finalize your details</h2>
    <form id="details" method="post" class="form">

        Full name: <strong name="name_1">ABCDEF</strong><br><br>
        ID No:<strong name="org_number_1">123</strong><br><br>
        Mobile No:<strong name="ph_number_1"">1234567890</strong><br><br>
        
        E-mail: <strong name="email_1">ABC@DEF.COM</strong><br><br>
        ID Card: <img src="profile.png" alt="preview" name="image" style="width: 100px; height: 100px;"><br><br>

        <button id="go">It's correct</button>
        
    </form>
Run Code Online (Sandbox Code Playgroud)

我的javascript:

document.getElementById('go').addEventListener('click', submit);
function submit(){

    var nme=document.getElementsByName("name_1")[0].innerHTML;
    var id=document.getElementsByName("org_number_1")[0].innerHTML;
    var phone=document.getElementsByName("ph_number_1")[0].innerHTML;
    var email=document.getElementsByName("email_1")[0].innerHTML;
    var img=document.getElementsByName("image")[0].src;

    const xhr=new XMLHttpRequest();

    xhr.onload=function(){

        const serverResponse=document.getElementById("response");
        serverResponse.innerHTML=this.responseText;
    };

    xhr.open("POST", "database_registration.php");
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send("name="+nme+"&id="+id+"&phone="+phone+"&email="+email+"&img="+img); //this line throws the error "Failed to load resource: the server responded with a status of 405 (Method Not Allowed)"
}
Run Code Online (Sandbox Code Playgroud)

我在Visual Studio Code 编辑器中执行我的代码,我的项目位置在:

C:\Users\Abhishek\Desktop\Web-dev stuff\XAMPP\htdocs\Stack hack 20 20

这是错误:

在此处输入图片说明

不仅如此,我还尝试使用Fetch API,但它也会引发相同的错误。现在我该怎么做?我怎样才能让它工作?

小智 0

我已经为您完成了第一个复选框(ID 号)。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cafteria details</title>
    
    <style>
        #special{
            height: 100px;
            width: 100px;
            border: 1px solid;
            display: none;
        }
    </style>
</head>
<body>
    <h2>Cafeteria registration</h2>
    <form class="details">
        Full name: <input type="text" placeholder="Please avoid any kind of prefixes" id="name" size=25><br><br>
        Organization:<div id="org"><input onclick="myFunction()" type="checkbox" id="cb1">ID no: <input type="number" id="org_number"><br><br>
                     <input type="checkbox" id="cb2">Mobile No: <input type="tel" id="ph_number"></div><br><br>
        
        E-mail: <input type="email" id="email" size=25><br><br>
        Upload ID Card: <input type="file" name="id" accept=".pdf,.jpeg" id="img"><br><br>
        
    </form>
    <div id ="special">

    </div>
    <button id="button">Register</button>
    
</body>
<script>


function myFunction() {
  var x = document.getElementById("cb1").checked;
  if (x == true) {
    document.getElementById("special").style.display="block";
  }else{
    document.getElementById("special").style.display="none";
  }
  
}

// var x = document.getElementById("cb1").checked;
//     if (x==true){
//     document.getElementById("special").style.display="block";
//     console.log(true)
// }
// if (document.getElementById("cb2").checked==true){
//     document.getElementById("special").style.display="block";
//     console.log(false)
// }
</script>
</html>
Run Code Online (Sandbox Code Playgroud)