JavaScript监听、设置全部ajax访问属性获取返回值(状态码)
JavaScript监听、设置全部ajax访问属性(requestHeader)获取返回值(状态码)
- 设置全部ajax访问属性
- 监听全部ajax返回值(状态码)
-
设置ajax属性(requestHeader)
$.ajaxSetup({ beforeSend: function(xhr) { if($.cookie('token') != undefined) { xhr.setRequestHeader('Authorization', 'bearer ' + $.cookie('token')) } } })
监听ajax返回值(状态码)
-
$.ajaxSetup({ contentType: "application/x-www-form-urlencoded;charset=utf-8", complete: function(XMLHttpRequest, textStatus) { }, statusCode: { 401: function() { alert('登录失效,请重新登录'); window.location.href = "login.html"; }, 504: function() { alert('数据获取/输入失败,服务器没有响应。504'); }, 500: function() { alert('服务器有误。500'); } } });
备注
需要引入jQuery,否则$.ajaxSetup无效
漫思