繁体   English   中英

XMLHTTPrequest发送空帖子

[英]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或者更正内容类型并在PHP中手动解析

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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