我可以为所有 AJAX 请求设置全局标头吗?

2022-01-11 00:00:00 header jquery javascript ajax

这似乎不起作用:

$.ajaxSetup({
  headers: {
    Accept: 'application/vvv.website+json;version=1 ',
    Authorization: 'Token token="FuHCLyY46"'
  }
});

我本来以为会的.如果我将这些过滤器专门添加到我的 AJAX 调用中,那么它们确实可以工作.我想对所有 AJAX 调用全局执行此操作.

I would have thought it would. If I add these filters specifically to my AJAX call then they do work. I'd like to do this globally for all AJAX calls.

推荐答案

我做了一些额外的测试,你发布的代码运行良好.如果您在设置参数时遇到问题,您可以随时调用 beforeSend 并自己修改 xml 请求.

I did some additional tests and the code you posted works perfectly. If you have problems with something in how the parameters are setup, you could always to go the beforeSend call and modify the xml request yourself.

$.ajaxSetup({
    beforeSend: function (xhr)
    {
       xhr.setRequestHeader("Accept","application/vvv.website+json;version=1");
       xhr.setRequestHeader("Authorization","Token token="FuHCLyY46"");        
    }
});

相关文章