【问题标题】:Origin http://localhost:1716 is not allowed by Access-Control-Allow-OriginAccess-Control-Allow-Origin 不允许来源 http://localhost:1716
【发布时间】:2012-03-22 04:48:33
【问题描述】:

我有这个错误

XMLHttpRequest 无法加载 http://localhost:81/Test/Service.svc/SetJSON。 Access-Control-Allow-Origin 不允许来源http://localhost:1716。

当我使用 jquery 调用 wcf Web 服务时。

$.ajax({
    type: 'POST',
    url: webmethod,
    data: '{"data":"Darshana"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        alert(msg.d);
    },
    error: function (e) {
        alert("Unavailable");
    }
});

这是为什么呢?

任何帮助。谢谢。。

【问题讨论】:

  • 我猜这是因为端口不同

标签: jquery json wcf iis


【解决方案1】:

本质上,因为您访问的是不同的端口,所以它不在同一个 origin 上。

您需要在端口 1716 上运行的服务上启用跨域资源共享。

不知道你想做什么,像this这样的配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 </system.webServer>
</configuration>

应该允许您进行测试,但对于生产来说,建议将适当的域列入白名单而不是使用通配符。

【讨论】:

  • 您需要根据您的配置对其进行自定义。
  • 为我工作。在 web.config 中为 ASP.net MVC4.5 项目添加了这个
【解决方案2】:

端口不同,尝试设置dataType: 'jsonp'

【讨论】:

【解决方案3】:

我通过 Apache mod_proxy 模块解决了这个问题。启用模块:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so

然后添加:

ProxyPass /your-get-url/ http://localhost:1716/

最后,将 proxy-url 传递给您的脚本。

【讨论】:

    【解决方案4】:

    我不记得我是如何得到这个错误的,什么时候出现的。但是,由于有很多人遇到这个问题,我想发布我所做的。

    WCF - IService

    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "SetJSON?data={data}")]
    string SetJSON(string data);
    

    WCF - 服务

    [AspNetCompatibilityRequirements(RequirementsMode = 
        AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service : IService
    {
        public string SetJSON(string data)
        {
            return data;
        }
    }
    

    WCF - web.config

    <system.serviceModel>
      <bindings>
        <webHttpBinding>
          <binding name="webHttpBindingWithJsonP"
                 crossDomainScriptAccessEnabled="true" />
        </webHttpBinding>
      </bindings>
    ....
    
    <services>
      <service name="RnDService.Service">
        <endpoint address="" behaviorConfiguration="webHttpBehavior"
            binding="webHttpBinding"
                  bindingConfiguration="webHttpBindingWithJsonP"
            contract="RnDService.IService" />
      </service>
    </services>
    

    jQuery 调用

    $.ajax({
        type: "GET",
        url: "http://localhost:81/Test/Service.svc/SetJSON?data=" + "{ \"dl\":" + datalist + " }",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        success: function (data) {
            alert(data.toString());
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            debugger;
            alert("Error Occured!");
        }
    });
    

    不是 100% 确定是什么解决了我的问题。无论如何,这将对某人有所帮助。 :)

    【讨论】:

      【解决方案5】:

      如果您在 Angular.js 中使用 localhost:port,请确保您像这样转义您的端口号:

      var Project = $resource(
             'http://localhost\\:5648/api/...',
                {'a':'b'}, 
                {
                  update: { method: 'PUT' }
                }
            );
      

      有关更多信息,请参阅https://github.com/angular/angular.js/issues/1243。

      【讨论】:

        【解决方案6】:

        我写了一个blog post (Accessing JSON data from a “local” data source is not permitted),它本质上是zenio 的解决方案,但有分步说明。您在 Apache 中设置了一个反向代理,以便它将本地 URL 转发到托管 JSON 数据的端口(基本上,将其转发到“远程”数据源)。

        【讨论】:

        • 这篇博文显然被标记为私有,所以我看不到它。没用。
        猜你喜欢
        • 2013-04-09
        • 2012-09-22
        • 1970-01-01
        • 1970-01-01
        • 2014-07-14
        • 2012-12-28
        • 2013-07-20
        • 2012-11-04
        相关资源
        最近更新 更多