2 回答

TA貢獻(xiàn)1875條經(jīng)驗 獲得超3個贊

TA貢獻(xiàn)1853條經(jīng)驗 獲得超9個贊
正如已經(jīng)指出的那樣,您可以嘗試fetch但以下代碼示例也可能對您有用 - 同時。只需使用正確的參數(shù)調(diào)用ajaxPOST(postParams, url) 。
//This function creates cross-browser XMLHttp object
function createXMLHttp(){
if (typeof XMLHttpRequest != "undefined") {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
var aVersions = [ "MSXML2.XMLHttp.9.0",
"MSXML2.XMLHttp.8.0",
"MSXML2.XMLHttp.7.0",
"MSXML2.XMLHttp.6.0",
"MSXML2.XMLHttp.5.0",
"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp","Microsoft.XMLHttp"
];
for (var i = 0; i < aVersions.length; i++) {
try {
var oXmlHttp = new ActiveXObject(aVersions[i]);
return oXmlHttp;
} catch (oError) {
//Do nothing
}
}
}
//if it reaches here, this cannot be created
throw new Error("XMLHttp object could be created.");
}
//this does Ajax POST
//sample params: var postParams = "key1="+value1+"&key2="+value2+"&keyx="+valuex;
function ajaxPOST(postParams, url){
var retString = false;
//creating xbrowser xmlhttp object; you handle any failures here
var http = createXMLHttp();
//setting headers
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//call-back handler
http.onreadystatechange = function() {
if(http.readyState == 4) {
if(http.status == 200) {
//this went fine
}
//read results now, you may trim it for extra spaces
retString = http.responseText;
//alert("retString: "+ retString);
}
return retString;
}
//send the request
http.send(postParams);
}
添加回答
舉報