启用Azure的网络CORS [英] Enable CORS in Azure web

查看:238
本文介绍了启用Azure的网络CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么我正尝试做的是使CORS(跨域资源共享),用于.NET MVC 5 Azure的网站调用从我的JavaScript HTTPS服务(不是我自己)时。

我总是得到同样的错误


  

XMLHtt prequest无法加载 https://开头someservice-I具备的,无控制-over 。无访问控制允许来源标头的请求的资源present。因此出身 https://my.azurewebsites.net '是不允许访问。响应有HTTP状态code 400。


我设法在本地开发时,我的项目设置为启用该 HTTPS 并加入以下的web.config

 < system.webServer>
< httpProtocol>
  < customHeaders>
    <添加名称=访问控制允许来源VALUE =*/>
    <添加名称=访问控制允许的方法VALUE =GET,POST,选项/>
    <添加名称=访问控制 - 允许 - 头VALUE =Content-Type的,接受,SOAPAction报/>
    <添加名称=访问控制-max-age的值=1728000/>
  < / customHeaders>
< / httpProtocol>
< /system.webServer>

这增加了访问控制允许来源标头。但是,这似乎并没有在Azure网站上运行。

和我不能找到像在移动服务的任何设置,你可以让这就像你在这里看到。

因为我知道你们都去索要code(即本地工作的BTW)有你有简单的jQuery调用服务

  $。阿贾克斯({
    网址:'https://开头someservice-I具备的,没有控制过',
    数据类型:JSON,
    的contentType:应用/ JSON,
    输入:GET,
    成功:函数(响应){
        $。每个(响应,功能(键,值){
          的console.log(成功); // Doesn't发生! :-(
        });
    },
    错误:功能(XHR,文字错误){
        如果($ .isFunction(onError的)){
            onError的(xhr.responseJSON);
        }
    }
});

因此​​,任何想法?

修改1

我只想澄清一点。

我打电话,我有比没有控制是一个HTTPS之一,在JavaScript(不是控制器)一种服务,是我的。

编辑2

好吧,我以为我可以拦截来自第三方服务的响应和浏览器拒绝它之前添加此头。在我看来这是不可能的(是吗?)。 但是怎么来的本地工作

如果我捕捉到的呼叫与如的LiveHTTPHeaders 我得到的以下响应,其中没有一个访问控制允许原产地的限制(这样的方式它在本地工作?)。

请求(为 https://开头someservice-I具备的,没有控制过.COM

  GET / someservice-I具备的,没有控制过/ SomeAction / 44 HTTP / 1.1
主持人:someservice-I-have-no-control-over.com
用户代理:Mozilla的/ 5.0(Windows NT的6.3; WOW64; RV:37.0)的Gecko / 20100101火狐/ 37.0
接受:text / html的,是application / xhtml + xml的,应用/ XML; Q = 0.9 * / *; Q = 0.8
接受语言:EN-US,EN; Q = 0.5
接受编码:gzip,紧缩
连接:保持活动
如果 - 无 - 匹配53867cff-96b0-411f-88b7-d84765f9f8e8
缓存控制:最大年龄= 0

回复

  HTTP / 1.1 304未修改
缓存控制:最大年龄= 900
服务器:Microsoft-IIS / 8.5
的X ASPNET-版本:4.0.30319
的X已启动方式:ASP.NET
访问控制允许的方法:GET,POST,PUT,DELETE,OPTIONS
日期:星期二,2015年2月24日11时06分53秒GMT


解决方案

不可能的。

据本地工作,因为它是在服务器必须有允许头,当你从你的JavaScript调用自己的Web服务器,您可以添加这些头。

当你调用真正的网站,他们也可能不添加CORS允许头(访问控制允许来源)和您的要求,因此被拒绝。

你可以做的是要么通过自己的网站上使用JSONP或代理的所有请求。

您可以例如使用我的CORS代理: https://github.com/jgauffin/corsproxy 。它的设计用途是IE9以下,但作品一样罚款的所有请求。

What I´m trying to do is to enable CORS (Cross-origin Resource Sharing) for .net MVC 5 Azure website when calling a https service (not my own) from my JavaScript.

I always get the same error

XMLHttpRequest cannot load https://someservice-I-have-no-control-over. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://my.azurewebsites.net' is therefore not allowed access. The response had HTTP status code 400.

I have managed to enable this when developing locally, setting my project to https and adding the following to web.config

<system.webServer>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS"/>
    <add name="Access-Control-Allow-Headers" value="Content-Type, Accept, SOAPAction"/>
    <add name="Access-Control-Max-Age" value="1728000"/>
  </customHeaders>
</httpProtocol>
</system.webServer>

That adds the 'Access-Control-Allow-Origin' header. But that does not seem to work on the Azure website.

And I can´t find any settings like in the Mobile Services where you can allow this like you see here.

Since I know you are all going to ask for code (that works locally btw) there you have the simple Jquery call to the service

$.ajax({
    url: 'https://someservice-I-have-no-control-over',
    dataType: 'json',
    contentType: 'application/json',
    type: 'GET',
    success: function (response) {
        $.each(response, function (key, value) {
          console.log("success"); //Doesn´t happen! :-(
        });
    },
    error: function (xhr, text, error) {
        if ($.isFunction(onError)) {
            onError(xhr.responseJSON);
        }
    }
});

So any thoughts?

Edit 1

Just to clarify a little.

I am calling a service that I have no control over that is a https one, in a javascript (not a controller) that is mine.

Edit 2

Ok I thought that I could intercept the response from the third party service and add this header before the browser rejects it. As I see it that is not possible (right?). But how come it works locally?

If I capture the call to this service with e.g LiveHTTPHeaders I get the following response where there is not a "Access-Control-Allow-Origin" restriction (so way does it work locally?).

Request (to https://someservice-I-have-no-control-over.com)

GET /someservice-I-have-no-control-over/SomeAction/44 HTTP/1.1
Host: someservice-I-have-no-control-over.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
If-None-Match: "53867cff-96b0-411f-88b7-d84765f9f8e8"
Cache-Control: max-age=0

Reply

HTTP/1.1 304 Not Modified
Cache-Control: max-age=900
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Date: Tue, 24 Feb 2015 11:06:53 GMT

解决方案

Not possible.

It works locally because it's the server that must have the allow headers, and when you call your own webserver from your javascript you can add those headers.

When you call the real website they do probably not add the CORS allow header (Access-Control-Allow-Origin) and your request is therefore denied.

What you could do is either to use JSONP or proxy all requests through your own website.

You could for instance use my CORS proxy: https://github.com/jgauffin/corsproxy. It's intended usage is for IE9 and below, but works just as fine for all requests.

这篇关于启用Azure的网络CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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