小编Saf*_*ami的帖子

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

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

以下是代码: -

$.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);
    }
});

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

然后我使用了Requestly(Requestly是chrome + firefox插件,我们可以手动为请求添加标头).

手动添加标题后: -

手动添加标题后

在两个图片请求标题中,x-auth-token存在于"ACCESS-CONTROL-REQUEST-HEADERS"中,但是"X-AUTH-TOKEN"标题连同标题值存在于第二pic中,而第二pic中不存在第一pic.

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

ajax header request-headers jquery-ajaxq

13
推荐指数
1
解决办法
3万
查看次数

标签 统计

ajax ×1

header ×1

jquery-ajaxq ×1

request-headers ×1