![](/img/trans.png)
[英]Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin
[英]Origin http://localhost:1716 is not allowed by Access-Control-Allow-Origin
我有這個錯誤
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");
}
});
這是為什么?
任何幫助。 感謝名單..
基本上,因為您正在訪問不同的端口,所以它不在同一個源上 。
您需要在端口1716上運行的服務上啟用跨源資源共享。
不知道你正在嘗試做的,配置像什么這樣 :
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
應該允許您進行測試,但是對於生產,建議將相應的域列入白名單而不是使用通配符。
不同的端口,嘗試設置dataType: 'jsonp'
我不記得我是怎么得到這個錯誤的。 但是,由於很多人遇到這個問題,我想發布我的所作所為。
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%肯定什么解決了我的問題。 無論如何,這將有助於某人。 :)
我通過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傳遞給您的腳本。
如果你在Angular.js中使用localhost:port,那么請確保你像這樣轉義你的端口號:
var Project = $resource(
'http://localhost\\:5648/api/...',
{'a':'b'},
{
update: { method: 'PUT' }
}
);
有關它的更多信息,請參閱https://github.com/angular/angular.js/issues/1243 。
我寫了一篇博文(不允許從“本地”數據源訪問JSON數據) ,這本質上是zenio的解決方案,但是有逐步指導。 您在Apache中設置了反向代理,以便將本地URL轉發到承載JSON數據的端口(基本上,將其轉發到“遠程”數據源)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.