Reputation: 2548
I have this error
XMLHttpRequest cannot load
http://localhost:81/Test/Service.svc/SetJSON
. Originhttp://localhost:1716
is not allowed by Access-Control-Allow-Origin.
when I calls wcf web service using jquery.
$.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");
}
});
why is that?
any help. thanx..
Upvotes: 4
Views: 16234
Reputation: 3617
If you use localhost:port in Angular.js, then make sure you escape your port number like this:
var Project = $resource(
'http://localhost\\:5648/api/...',
{'a':'b'},
{
update: { method: 'PUT' }
}
);
See https://github.com/angular/angular.js/issues/1243 for more info on it.
Upvotes: 0
Reputation: 9
I've written a blog post (Accessing JSON data from a “local” data source is not permitted) that is essentially zenio's solution, but with step-by-step directions. You set up a reverse proxy in Apache so that it forwards a local URL to the port that hosts the JSON data (basically, forwarding it to the "remote" data source).
Upvotes: -1
Reputation: 2548
I don't remember how I got this error and when. But, as lots of people having this problem I thought to post what I did.
WCF - IService
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "SetJSON?data={data}")]
string SetJSON(string data);
WCF - Service
[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 call
$.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!");
}
});
not 100% sure what solved my problem. Anyway this will help someone. :)
Upvotes: 1
Reputation: 854
I solved this by Apache mod_proxy module. Enable modules:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Then add:
ProxyPass /your-get-url/ http://localhost:1716/
Finally, pass proxy-url to your script.
Upvotes: 1
Reputation: 2967
Essentially, because you're accessing a different port it's not on the same origin.
You'll need to enable Cross Origin Resource Sharing on the service that runs on port 1716.
Without knowing what you're trying to do, a configuration like 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>
Should allow you to test, although for production it's advisable to whitelist appropriate domains rather than using a wildcard.
Upvotes: 3