为什么ajax post不起作用


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();
    });

上面的javascript代码是javascript函数,通过点击id=MainContent_btnSave按钮来调用。

带有GET参数的相同请求成功完成。

但是带参数POST xmlhttp。status总是等于405,并出现类似这样的错误:"用于'send.php'的命令HTTP POST被拒绝。"有什么问题吗?

文件'send.php'包含:

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

如果你正在使用jQuery,那么使用它的函数。$.post()例如:

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

参数POST xmlhttp。status总是等于405,并出现类似这样的错误:"用于'send.php'的命令HTTP POST被拒绝。"

405错误是Method Not Allowed。这意味着:你的web服务器正在阻止POST请求,或者你的PHP框架正在阻止它,或者没有为send.php定义POST路由。