XMLHTTPrequest 发送空 post

XMLHTTPrequest sending empty post

我之前做过很多次,所以老实说我很困惑为什么这没有通过任何东西。我试过打印结果(脚本得到响应并打印文件)。

function submit_form_inpage(path, data, watchForChange){
    alert(data);
    watchForChange = watchForChange || false;
    var request = new XMLHttpRequest();
    request.open('POST', path, true);
    request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    if (watchForChange == true) {
        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                document.write(request);
                if (request.status==200 && request.status < 400){
                    var xmldata=request.responseText //retrieve result as an XML object
                    alert("XML:" + xmldata);
                }
                else{
                    alert("An error has occured making the request:" + request.status );
                }
            }
        }
    }
    var temp_string = array_to_string_for_post(data);
    var temp = JSON.stringify(data);
    alert(temp);
    request.send(temp);
}

我的 php 是

print_r($_POST);

我的结果是

XML: Array ()

尽管传入的数据(在我的警报发送之前经过双重检查)是

{"reason":"get_stuff","build_name":"test"}

你说你发送的是表单编码数据。

request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

那么你发送了:

temp = JSON.stringify(data);

JSON 是 application/json 而不是 application/x-www-form-urlencoded(而且 PHP 本身不支持)。

将您的数据编码为 application/x-www-form-urlencoded 或更正您的内容类型和 parse it manually in PHP