开发者社区 问答 正文

jquery ajax发送Post请求,如何添加请求头

如题,原生的ajax中xhr对象有setRequestHeader()方法,jquery中如何设置?

展开
收起
a123456678 2016-07-06 14:29:29 7867 分享 版权
1 条回答
写回答
取消 提交回答
  • // attempt to make an XMLHttpRequest to indeed.com
    // jQuery 1.6.1  and Chrome  dev 13.0.767
    function makeRequest() {
        alert("inside makeRequest()");
        var settings = {
            type: "GET",
            url:URL+"?"+REQUEST,
            dataType:"xml",
            error: function(XHR,textStatus,errorThrown) {
                alert ("XHR="+XHR+"\ntextStatus="+textStatus+"\nerrorThrown=" + errorThrown);
            },
            success: function(data,textStatus) {
                $("body").append(data);
            },
            headers: {
                "Access-Control-Allow-Origin":"http://example.edu",
                "Access-Control-Allow-Headers":"X-Requested-With"
            }
        };
        $.ajax(settings);
    }
    2019-07-17 19:52:06
    赞同 展开评论