2012-03-22 71 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

相關問題