简体   繁体   中英

Why ajax post didn't work?

    $("#MainContent_btnSave").click(function (e) {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    $('#myDiv').text(xmlhttp.responseText);
    }
    }
    xmlhttp.open("POST", "send.php", true);
    xmlhttp.send();
    e.preventDefault();
    });

The javascript code above is javascript function, which is called by clicking on button with id=MainContent_btnSave.

The same request with parameter GET finish successfully.

But with parameter POST xmlhttp.status always equal to 405 and arise error like that: "Command HTTP POST, used to 'send.php', is denied." What can be the problem?

File 'send.php' contain :

    <?php
   echo "Your email was sent!";
    ?>

If you're using jQuery then use it's functions. $.post() for example:

$("#MainContent_btnSave").click(function() {
    $.post('send.php', data, function(response) {
         $("#myDiv").text(response);
    });
});

parameter POST xmlhttp.status always equal to 405 and arise error like that: "Command HTTP POST, used to 'send.php', is denied."

A 405 error is Method Not Allowed. This means either: your webserver is blocking the POST request, or your PHP framework is blocking it, or doesn't have a POST route defined for send.php.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM