问题描述
我已经做过很多次了,所以我很困惑为什么它没有通过任何事情.我已经尝试过打印结果(脚本会得到响应并打印文件).
I've done a lot of times before, so I'm honestly confused why this is failing to pass anything. I've tried printing results (script gets a response and prints the file).
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 ()
尽管事实是传入的数据(在我的警报发送之前已对其进行仔细检查)是
Despite the fact that data passed in (which is double-checked right before being sent by my alert) is
{"reason":"get_stuff","build_name":"test"}
推荐答案
您说过您正在发送表单编码的数据.
You said you were sending form encoded data.
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本身也不支持).
JSON is application/json
not application/x-www-form-urlencoded
(and isn't natively supported by PHP anyway).
要么将数据编码为application/x-www-form-urlencoded
,要么更正内容类型并在PHP中手动解析.
Either encode your data as application/x-www-form-urlencoded
or correct your content-type and parse it manually in PHP.
这篇关于XMLHTTPrequest发送空帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!