如何在 Jquery Ajax 中向请求添加标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41929654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:20:37  来源:igfitidea点击:

How to add header to request in Jquery Ajax?

ajaxheaderrequest-headersjquery-ajaxq

提问by Safdar Akrami

I'm trying to add header to request in Ajax with JQuery.

我正在尝试使用 JQuery 在 Ajax 中向请求添加标头。

Below is the code :-

以下是代码:-

$.ajax({
    type: "POST",
    contentType: "application/json",
    url: "http://localhost:8080/core-service/services/v1.0/patients/registerPatients",
    data: JSON.stringify(patientDTO),
    //crossDomain : true,
    dataType: 'json',
    headers: {"X-AUTH-TOKEN" : tokken},
    success: function(patientDTO) {
        console.log("SUCCESS: ", patientDTO);
        /* location.href = "fieldagentHRA.html";*/
        if (typeof(Storage) !== "undefined") {
            localStorage.setItem("patUrn", patientDTO.data);
            location.href="fieldagentHRA.html";
        }
    },
    error: function(e) {
    console.log("ERROR: ", e);
    display(e);
    },
    done: function(e) {
    enableRegisterButton(true);
    }
});

I inspected this with chrome and found that header's body is not being added.Without Header Body

我用 chrome 检查了这个,发现标题的正文没有被添加。没有标题正文

Then I used Requestly(Requestly is chrome+firefox plugin with which we can manually add a header to the request).

然后我使用了RequestlyRequestly 是 chrome+firefox 插件,我们可以使用它手动向请求添加标头)。

After manually adding header :-

手动添加标题后:-

After Manually Adding the header

手动添加标题后

In both the pics request header x-auth-token is present in "ACCESS-CONTROL-REQUEST-HEADERS" but "X-AUTH-TOKEN" header along with header value is present in second pic which is not there in the first pic.

在两个图片请求标头中,x-auth-token 都存在于“ACCESS-CONTROL-REQUEST-HEADERS”中,但“X-AUTH-TOKEN”标头以及标头值出现在第二张图片中,而第一张图片中没有。

So my question is how to add request headers in Ajax with JQuery ?

所以我的问题是如何使用 JQuery 在 Ajax 中添加请求标头?

回答by Soni Vimalkumar

There are couple of solutions depending on what you want to do

根据您要执行的操作,有几种解决方案

If want to add a custom header (or set of headers) to an individual request then just add the headersproperty and this will help you to send your request with headers.

如果要将自定义标头(或标头集)添加到单个请求,则只需添加该headers属性,这将帮助您发送带有标头的请求。

// Request with custom header
$.ajax({
    url: 'foo/bar',
    headers: { 'x-my-custom-header': 'some value' }
});

If want to add a default header (or set of headers) to every request then use $.ajaxSetup():this will help you to add headers.

如果想为每个请求添加一个默认标头(或一组标头),那么使用$.ajaxSetup():这将帮助您添加标头。

//Setup headers here and than call ajax
$.ajaxSetup({
    headers: { 'x-my-custom-header': 'some value' }
});

// Sends your ajax
$.ajax({ url: 'foo/bar' });

add a header (or set of headers) to every request then use the beforeSend hook with $.ajaxSetup():

向每个请求添加一个标头(或一组标头),然后将 beforeSend 钩子与 $.ajaxSetup() 一起使用:

//Hook your headers here and set it with before send function.
$.ajaxSetup({
    beforeSend: function(xhr) {
        xhr.setRequestHeader('x-my-custom-header', 'some value');
    }
});

// Sends your ajax
$.ajax({ url: 'foo/bar' });

Reference Link : AjaxSetup

Reference Link : AjaxHeaders

参考链接:AjaxSetup

参考链接:AjaxHeaders