POST 请求必须包含一些标头:
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", params.length);
xhr.setRequestHeader("Connection", "close");
编辑 :
我的脚本使用一个名为“params”的变量。因此,在脚本的开头添加一个 var 并将您的请求内容放入其中:
var params = "info="+str+"&info_1="+info_1+"&info_2="+info_2;
并在调用 xhr.send "info="+str+"&info_1="+info_1+"&info_2="+info_2; 通过参数:
xhr.send(params);
这是最终的脚本:
var xhr = new XMLHttpRequest();
var params = "info="+str+"&info_1="+info_1+"&info_2="+info_2;
xhr.open("POST",url,true);
xhr.onreadystatechange = function() {
if( this.readyState == 4 && this.status == 200) {
document.getElementById(name).innerHTML = this.responseText;
}
};
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", params.length);
xhr.setRequestHeader("Connection", "close");
xhr.send(params);
return false;