当前位置: 代码网 > it编程>前端脚本>Ajax > ajax设置header指南教程

ajax设置header指南教程

2024年05月15日 Ajax 我要评论
什么是 ajax?ajax = asynchronous javascript and xml.ajax 并非编程语言。ajax 仅仅组合了:浏览器内建的 xmlhttprequest 对象(从 we

什么是 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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com