jQuery'的JavaScript等价物;s$.ajax函数
我有以下jQuery代码:jQuery'的JavaScript等价物;s$.ajax函数,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有以下jQuery代码: dataString = 'test'; // array? $.ajax({ type: "POST", url: "tokenize.php", data: { data: dataString }, cache: false, success: function (data) { returnedvalue = data; console.log(data); //
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);
application/x-www-form-urlencoded
数据的格式为:
key=value&key=value&key=value
运行每个键和值,以处理具有特殊含义或编码中不允许的字符。查看此演示。谢谢,但我的问题不同。我有样品,但我应该如何发送我的参数?我需要一篇博文:使用jQuery这样的库来发出AJAX请求几乎总是更好的,因为它可以为您抽象出浏览器之间的差异。如果您打算直接使用XMLHttpRequest,我建议您从以下文档开始: