什么是 AJAX?
AJAX = Asynchronous JavaScript And XML.
AJAX 并非编程语言。
AJAX 仅仅组合了:
浏览器内建的 XMLHttpRequest 对象(从 web 服务器请求数据)
JavaScript 和 HTML DOM(显示或使用数据)
Ajax 是一个令人误导的名称。Ajax 应用程序可能使用 XML 来传输数据,但将数据作为纯文本或 JSON 文本传输也同样常见。
Ajax 允许通过与场景后面的 Web 服务器交换数据来异步更新网页。这意味着可以更新网页的部分,而不需要重新加载整个页面。
下面介绍下ajax设置header指南教程,内容如下所示:
setting参数 headers
1 2 3 4 5 6 7 8 | $.ajax({ headers: { Accept: "application/json; charset=utf-8" }, type: "get" , success: function (data) { } }); |
beforeSend设置header
1 2 3 4 5 6 7 8 9 10 | $.ajax({ type: "GET" , url: "default.do" , beforeSend: function (request) { request.setRequestHeader( "Test" , "Chenxizhang" ); }, success: function (result) { alert(result); } }); |
$.ajaxSetup()全局设置Header请求头
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // 设置请求默认值 $.ajaxSetup({ beforeSend: function (xhr) { //可以设置自定义标头 // 将token塞进Header里 xhr.setRequestHeader( 'Authorization' , 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' ); xhr.setRequestHeader( 'Content-Type' , 'application/json' ); // application/x-www-form-urlencoded }, complete: function (xhr) { // 设置登陆拦截 if (xhr.responseJSON.code == "error_unauth" ) { console.log( "没有登录!" ); layer.msg( "没有登录!" ); // location.href="login.html" rel="external nofollow" rel="external nofollow" ; } else { console.log( "已经登录!" ); } }, }); |
或
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // 设置请求默认值 $.ajaxSetup({ headers: { // 默认添加请求头 "Authorization" : "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" , "Content-Type" : "application/json" } , complete: function (xhr) { // 设置登陆拦截 if (xhr.responseJSON.code == "error_unauth" ) { console.log( "没有登录!" ); layer.msg( "没有登录!" ); // location.href="login.html" rel="external nofollow" rel="external nofollow" ; } else { console.log( "已经登录!" ); } }, }); |
到此这篇关于ajax设置header的文章就介绍到这了,更多相关ajax设置header内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!