基础

  1. 发送数据的内容编码类型contentType 默认值为 application/x-www-form-urlencoded,也可以指定其它类型,例如 application/json

Ajax 添加 Token 到 Request Header 中

方法一

1
2
3
4
5
$.ajax({
type: "GET",
url: "/access/logout/" + userCode,
headers: {'Authorization': token}
});

方法二

1
2
3
4
5
6
7
8
9
$.ajax({
type: "GET",
url: "/access/logout/" + userCode,
beforeSend: function(request) {
request.setRequestHeader("Authorization", token);
},
success: function(result) {
}
});

使用 formData 方法来使用 Ajax 请求向服务端上传 form 表单附件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$("#submit").click(function(){  
var formData = new FormData($("#form")[0]);
$.ajax({
url:"http://localhost:8080/file/servlet/UploadHandleServlet",
type:"post",
data:formData,
async: false,
cache: false,
contentType: false,
processData: false,
success:function(){
// something to do...
}
});
});

ajax 超时设置在同步模式无效,必须设置为 async:true 异步执行才行