ajax规范

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
26
27
28
29
30
31
32
$.ajax({
url: '',
type: '',
dataType: 'json',
timeout: 3000,
data: {
num1: num1,
num2: num2
},
beforeSend: function(xhr, settings) {
// 发送请求前
console.log('beforeSend', xhr, settings);
},
success: function(data, status, xhr) {
// 请求成功
console.log('success', data, status, xhr);
if (data.code === 200) {
console.log('处理成功');
} else {
console.log('处理失败');
}
},
error: function(xhr, errorType, error) {
// 请求失败
console.log('error', xhr, errorType, error);
alert('请求失败');
},
complete: function(xhr, status) {
// 请求成功或失败都执行
console.log('complete', xhr, status);
}
});