2012-03-22 70 views
4

让我有这样的错误产地的http://本地主机:1716没有被访问控制允许来源

的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"); 
    } 
}); 

这是为什么?

任何帮助。 thanx ..

+0

这是因为该端口是不同的我想 – 2012-03-22 04:51:36

回答

1

我不记得我是如何得到这个错误和什么时候。但是,正如很多有这个问题的人,我想要发布我所做的。

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%确定什么解决了我的问题。无论如何,这将有助于某人。 :)

3

基本上,因为您正在访问不同的端口,它不在相同的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> 

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

+0

我这样做,但不工作。 thanx – Darshana 2012-03-22 05:11:29

+0

您需要根据您的配置对其进行自定义。 – djlumley 2012-03-22 07:03:12

+1

为我工作。在web.config中增加了这一个ASP.net MVC4.5项目 – 2013-03-06 21:53:59

2

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

+0

然后是这个GET HTTP://本地主机:81 /测试/ Service.svc/SetJSON回调= jQuery1710770164709771052_1332392664912&{%22data%22:%22Darshana 22% }&_ = 1332392682691 400(错误请求)错误 – Darshana 2012-03-22 05:09:22

1

我被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/ 

最后,通过代理网址到你的脚本。

-1

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

+0

博客文章显然标记为私人,所以我看不到它。没有用。 – gpvos 2014-02-24 15:55:28

0

如果在Angular中使用localhost:port。JS,然后确保你逃避你的端口号,这样的:

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

关于它的详细信息请参见https://github.com/angular/angular.js/issues/1243

相关问题