什么是 ajax?
ajax = asynchronous javascript and xml.
ajax 并非编程语言。
ajax 仅仅组合了:
浏览器内建的 xmlhttprequest 对象(从 web 服务器请求数据)
javascript 和 html dom(显示或使用数据)
ajax 是一个令人误导的名称。ajax 应用程序可能使用 xml 来传输数据,但将数据作为纯文本或 json 文本传输也同样常见。
ajax 允许通过与场景后面的 web 服务器交换数据来异步更新网页。这意味着可以更新网页的部分,而不需要重新加载整个页面。
下面介绍下ajax设置header指南教程,内容如下所示:
setting参数 headers
$.ajax({ headers: { accept: "application/json; charset=utf-8" }, type: "get", success: function (data) { } });
beforesend设置header
$.ajax({ type: "get", url: "default.do", beforesend: function(request) { request.setrequestheader("test", "chenxizhang"); }, success: function(result) { alert(result); } });
$.ajaxsetup()全局设置header请求头
// 设置请求默认值 $.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("已经登录!"); } }, });
或
// 设置请求默认值 $.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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论