Ajax

AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。
AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。

使用教程:
创建 XMLHttpRequest 对象

1
2
3
4
5
6
7
8
9
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

向服务器发送请求

1
2
xmlhttp.open(method,url,async);
xmlhttp.send(string);

服务器响应
responseText 或 responseXML

Paste_Image.png

JQuery ajax

jQuery.ajax([settings])

项目中使用到的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$.ajax({
url: apiUrl + apiName,
type: 'post',
data: jsonData,
cache: false,
dataType: 'json',
contentType: 'application/json',
//timeout: 1000,
async: asyn,
beforeSend: function (XMLHttpRequest) {

if (showLoading) nopCommerce.app.loadPanel.show(loadingMessage); //Show Loading
XMLHttpRequest.setRequestHeader("Client-Referrer", document.referrer);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("---------ERROR---------", new Date())
console.log("ERROR", apiName, { request: XMLHttpRequest, textStatus: textStatus, errorThrown: errorThrown });
},
success: function (result) {
resultHandle(apiName, data, done, fail, showLoading, loadingMessage, result, startTime, isWorker);
},
complete: function () {
if (showLoading) nopCommerce.app.loadPanel.hide() //close loading
}
});


资料推荐
$.ajax()方法详解