JavaScript 原生 ajax
注意浏览器兼容性判断
var http = new XMLHttpRequest();
http.open("method", "url", true);//第三个参数 async
http.onreadystatechange = function () {
//ajax 状态值判断
if (http.readyState == "4") {
//ajax 状态码判断
if ((http.status >= 200 && http.status < 300) || http.status == 304){
console.log(http.responseText);
}
}
else {
console.log(http.status);
}
}
xml.send();
JQuery 类库的 ajax
$.ajax({
url: "url",
data: "data",
type: "method",
dataType: "json",
async: true,
beforSend: function () {
//请求之前
},
success: function (d) {
//成功
},
error: function (e) {
//出错
}
网友评论