0
我有以下的jQuery代碼:
dataString = 'test'; // array?
$.ajax({
type: "POST",
url: "tokenize.php",
data: {
data: dataString
},
cache: false,
success: function (data) {
returnedvalue = data;
console.log(data); //alert isn't for debugging
}
});
這個jQuery代碼工作正常,但我想一個普通的這段代碼的JavaScript版本這我不能圖出去怎麼辦。我只在Stack Overflow的幫助下編寫了這段代碼。
我已經看到,這可以使用XMLHttpRequest
來完成:
var http = new XMLHttpRequest();
var url = "tokenize.php";
var params = "lorem=ipsum&name=binny"; // What will be done here in my case?
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
// Call a function when the state changes.
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
查看本演示。 http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first – Zeeshan
謝謝,但我的問題是不同的。我有樣品,但我怎麼發送我的參數?我需要POST – Piya
你幾乎總是用像jQuery這樣的庫來製作AJAX請求,因爲它會爲你抽象出瀏覽器差異。如果你直接使用XMLHttpRequest,我建議你從這裏的文檔開始:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest –