除去在jQuery.ajaxSetup设置特定的请求头 [英] Remove specific request headers set in jQuery.ajaxSetup

查看:2533
本文介绍了除去在jQuery.ajaxSetup设置特定的请求头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置使用一些自定义标题

I setup some custom headers using

$.ajaxSetup({
    headers : {
        'x-custom' : 'value'
    }
});

这将增加 X-定制头为所有Ajax请求。但我想一些具体的要求,以不包含这个头。

It will addx-custom header for all the ajax request. But I want some specific requests to NOT contain this header.

我想这一点,这AJAX调用之前删除ajaxSettings头并将其添加回当它完成了

I tried this, delete header from ajaxSettings before that ajax call and add it back when its completed

delete $.ajaxSettings.headers["x-custom"];

$.ajax({
    ...
    "success": function (data) {
        $.ajaxSettings.headers["x-custom"] = 'value';
        ...
    }
});

但我觉得这是不正确的做法,因为在完成该呼叫不会得到该标题之前发射的要求。还有什么可我请建议。

But I feel this is not the correct way, as the request that fired before finishing that call will not get that header. What else can I do please suggest.

我应该添加的头背在下一行 $。阿贾克斯,而不是做在回调?

Should I add the header back in the next line after $.ajax instead doing it in callback?

推荐答案

由于这个问题不具有可被标记为接受任何回答。我张贴的解决方案。

Since this question doesn't have any answer that can be marked as Accepted. I am posting the solution.

看起来像AJAX调用后立即加回了头才有意义。这样,我们就不会在等待成功的回调,然后将它添加。

Looks like adding back the header immediately after the AJAX call would make sense. This way we won't be waiting for success callback and then adding it.

delete $.ajaxSettings.headers["x-custom"]; // Remove header before call

$.ajax({
    ...
    "success": function (data) {
        ...
    }
});

$.ajaxSettings.headers["x-custom"] = 'value'; // Add it back immediately

这篇关于除去在jQuery.ajaxSetup设置特定的请求头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆