Javascript XMLHttpRequest将有效负载附加到uri
我正在做一个简单的POST-XMLHttpRequest,它成功地发布了数据,同时也将paylod附加到了URL中。是否有数据没有附加到URLJavascript XMLHttpRequest将有效负载附加到uri,javascript,json,xmlhttprequest,Javascript,Json,Xmlhttprequest,我正在做一个简单的POST-XMLHttpRequest,它成功地发布了数据,同时也将paylod附加到了URL中。是否有数据没有附加到URL var payLoad= JSON.stringify(ItemJSON); var xmlhttp = new XMLHttpRequest();
var payLoad= JSON.stringify(ItemJSON);
var xmlhttp = new XMLHttpRequest();
xmlhttp.addEventListener("readystatechange", function () {
if (xmlhttp.readyState < 4){
}
else if (xmlhttp.readyState === 4) {
if (xmlhttp.status == 200 && xmlhttp.status < 300){
console.log("Success",xmlhttp.responseText);
}
}else{
alert("Error!! \\n There was an error while saving. Please retry again later.");
}
});
xmlhttp.open("POST", URL, false);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(payLoad);
}
var payLoad=JSON.stringify(ItemJSON);
var xmlhttp=new XMLHttpRequest();
addEventListener(“readystatechange”,函数(){
如果(xmlhttp.readyState<4){
}
如果(xmlhttp.readyState==4){
如果(xmlhttp.status==200&&xmlhttp.status<300){
log(“Success”,xmlhttp.responseText);
}
}否则{
警报(“错误!!\\n保存时出错。请稍后重试。”);
}
});
open(“POST”,URL,false);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
发送(有效载荷);
}
答复:
这是因为您在发布内容时使用的标题:
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
换成
xmlhttp.setRequestHeader("Content-Type", "application/json");
您的参考资料:感谢Sujith提供的帮助。这里的另一点是我在发布表单数据。但当我移除表单时,它起了作用。