1 /**
2 * 普通数据公共ajax请求
3 * @param url
4 * @param data
5 * @param type
6 * @returns {*}
7 */
8 function commonAjax(url, data, type) {
9 //必须在ajax外面定义,里面获取不到值
10 let result = null;
11
12 $.ajax({
13 url: 'http://localhost:8080' + url,
14 data: data,
15 type: type,
16 //async. 默认是 true,即为异步方式
17 //async 设置为 false,则所有的请求均为同步请求
18 //processData: false, 带file类型添加
19 //contentType: false, 带file类型添加
20 async: false,
21 xhrFields: {
22 //允许cookie跨域
23 widthCredentials: true
24 },
25 dataType: 'json',
26 success: function (data) {
27 //401状态码表示未登录
28 if (data === 401) {
29 alert("身份已过期!");
30 window.location.href = '/login.html';
31 }
32 result = data;
33 }
34 }
35 );
36 return result;
37 }