我可以在跨域 json 请求中设置标头吗? [英] Can I set headers in cross domain json requests?

查看:30
本文介绍了我可以在跨域 json 请求中设置标头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上做了一些研究,但我没有设法了解这个主题的完整情况.任何人都可以帮助现在和永远解决这个答案吗?

I have done some research on the internet, but I didn't manage to get the complete picture about this subject. Can anyone help to solve this answer for now and forever?

这是我目前发现的:

  • 可以使用 jsonp 进行跨域调用.永远不允许在 jsonp 调用中更改标头
  • 如果服务器允许,可以使用 json 进行跨域调用.

这就是我想要做的:

$.ajax({
    type: "GET",
    crossDomain: true,
    beforeSend: function (request) {
        request.setRequestHeader("Authorization", "Bearer " + ($("#accesstoken").val()));
    },
    contentType: "application/json; charset=utf-8",
    url: myJSonServer + encodeURI(operation),
    dataType: 'json',
    cache: false,
    success: callback,
    error: function (jqXhr, textStatus, errorThrown) { alert(textStatus + ": " + errorThrown); }
});

这是正在发生的事情:

  • 当myJSonServer在同一个域时,完全没有问题
  • 当 myJSonServer 在另一个域上时,会发送请求,但没有 Bearer 标头

此 Bearer 标头是 oAuth2 标准的一部分.

This Bearer header is part of the oAuth2 standard.

我知道在浏览器中设置 accessToken 可能不是最好的解决方案.我知道我可以在这种情况下使用代理.

I'm aware of the fact that maybe this is not the best solution, setting the accessToken in the Browser. And I know I could use a proxy for this situation.

我只是想知道是否可以或可以在跨域 json 请求上设置标头?
谢谢

I am just curious if it is or will be possible to set the headers on a cross-domain json request?
Thanks

我使用的是 MVC4 并在 web.config 中添加了 crossDomainScriptAccessEnabled="true".我认为这已经足够了,但是 apsillers 的回答解决了我的问题.我现在已经在我的 web.config 中添加了这个:

I was using MVC4 and added crossDomainScriptAccessEnabled="true" in the web.config. I thought this would be enough, but the answer of apsillers solved my problem. I have now added this in my web.config :

 <system.webServer>
     <httpProtocol>
         <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Authorization" />
         </customHeaders>
      </httpProtocol>
   </system.webServer>

推荐答案

使用 JSONP,无法设置自定义标头.

With JSONP, setting custom headers is not possible.

使用 CORS,服务器必须发送 Access-Control-Allow-Headers 标头以允许来自客户端的不常见请求标头.来自 HTML5 Rocks CORS 页面:

With CORS, the server must send the Access-Control-Allow-Headers header to allow uncommon request headers from the client. From the HTML5 Rocks CORS page:

Access-Control-Allow-Headers ... - 支持的请求标头的逗号分隔列表.

Access-Control-Allow-Headers ... - Comma-delimited list of the supported request headers.

因此,您的服务器必须发送 Access-Control-Allow-Headers: Authorization 以让浏览器知道允许将 Authorization 与请求一起发送到服务器.如果没有这个 sever 标头,浏览器只会随请求发送一些常见的标头,而忽略其余的.

Thus, your server must send a Access-Control-Allow-Headers: Authorization to let the browser know it is permissible to send Authorization to the server with the request. Without this sever header, the browser will only send a few common headers with the request and ignore the rest.

这篇关于我可以在跨域 json 请求中设置标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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