php& ajax重新发布帖子数据失败

时间:2016-01-07 21:53:15

标签: javascript php ajax xmlhttprequest

当我尝试通过php将帖子数据发送到xmlhttp request时,我遇到了一些非常奇怪的问题:

这是javascript代码:

function getHeaterDailyConfig(){
    var oReq = new XMLHttpRequest();
    var d = new Date()
    now = [d.getFullYear(), d.getMonth()+1, d.getDate()].join('-');
    var reqArgs = "isDateRequested=1&date=" + now;
    oReq.onload = function() {
        alert(this.responseText);
        daliyConfig = JSON.parse(this.responseText);
    };
    oReq.open("post", "getLatestCfgHeater.php", true);
    oReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    oReq.send(reqArgs);
}

和php:

error_log("start");
error_log(print_r( $_POST['isDateRequested']));
error_log(print_r( $_POST['date']));

error_log("stop");

不幸的是,php给了我非常奇怪的日志,看起来像从未收到过帖子数据:

[Thu Jan 07 22:44:16 2016] [error] [client 192.168.0.3] start, referer: http://192.168.0.12/fullcalendar-1.5.3/demos/sel3.html
[Thu Jan 07 22:44:16 2016] [error] [client 192.168.0.3] PHP Notice:  Undefined index: isDateRequested in /var/www/fullcalendar-1.5.3/demos/getLatestCfgHeater.php on line 6, referer: http://192.168.0.12/fullcalendar-1.5.3/demos/sel3.html
[Thu Jan 07 22:44:16 2016] [error] [client 192.168.0.3] 1, referer: http://192.168.0.12/fullcalendar-1.5.3/demos/sel3.html
[Thu Jan 07 22:44:16 2016] [error] [client 192.168.0.3] PHP Notice:  Undefined index: date in /var/www/fullcalendar-1.5.3/demos/getLatestCfgHeater.php on line 7, referer: http://192.168.0.12/fullcalendar-1.5.3/demos/sel3.html
[Thu Jan 07 22:44:16 2016] [error] [client 192.168.0.3] 1, referer: http://192.168.0.12/fullcalendar-1.5.3/demos/sel3.html
[Thu Jan 07 22:44:16 2016] [error] [client 192.168.0.3] stop, referer: http://192.168.0.12/fullcalendar-1.5.3/demos/sel3.html

有什么问题?

1 个答案:

答案 0 :(得分:0)

我试过这样的话。它可以正确发送数据:

<?php
error_reporting(E_ALL);

var_dump($_POST);
?>
<script>getHeaterDailyConfig();
function getHeaterDailyConfig(){
            var oReq = new XMLHttpRequest();
            var d = new Date()
            now = [d.getFullYear(), d.getMonth()+1, d.getDate()].join('-');
            var reqArgs = "isDateRequested=1&date=" + now;
                            oReq.onload = function() {
                            alert(this.responseText);
                            daliyConfig = JSON.parse(this.responseText);
            };
            oReq.open("post", "index.php", true);
            oReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            oReq.send(reqArgs);

    }

    </script>

我得到的数据是这样的:

array(2) {
  ["isDateRequested"]=>
  string(1) "1"
  ["date"]=>
  string(8) "2016-1-7"
}
<script>getHeaterDailyConfig();
function getHeaterDailyConfig(){
            var oReq = new XMLHttpRequest();
            var d = new Date()
            now = [d.getFullYear(), d.getMonth()+1, d.getDate()].join('-');
            var reqArgs = "isDateRequested=1&date=" + now;
                            oReq.onload = function() {
                            alert(this.responseText);
                            daliyConfig = JSON.parse(this.responseText);
            };
            oReq.open("post", "index.php", true);
            oReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            oReq.send(reqArgs);

    }

    </script>