Origin is not allowed by Access-Control-Allow-Origin.

Topics: jQuery
Dec 19, 2011 at 7:19 AM

When I run my solution on localhost from Visual Studio, my RIA/JS solution works fine:

                  $([itemArr]).dataSource({
                          serviceUrl: "http://localhost:51850/RiaJS-Demo-DemoService.svc",
                          queryName: "GetItems"
                      }).refresh();

But when I deploy my services to the cloud, I get the following error:

XMLHttpRequest cannot load http://myapp.cloudapp.net/Services/RiaJS-Demo-DemoService.svc/json/GetItems. Origin http://localhost:51850 is not allowed by Access-Control-Allow-Origin. (at least I can see this error in chrome console, other Browsers don't show anything)

I have already tried to put the following code into the service implementation, which I have found in this thread: http://stackoverflow.com/questions/4875195/problem-sending-json-data-from-jquery-to-wcf-rest-method

if (WebOperationContext.Current.IncomingRequest.Method == "OPTIONS")
{
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin""*");
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods""POST");
     WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers""Content-Type, Accept");
 
     return null;
}

But it still shows the same error. Any ideas?



When I run my solution on localhost from Visual Studio, my RIA/JS solution works fine:

                  $([itemArr]).dataSource({
                          serviceUrl: "http://localhost:51850/RiaJS-Demo-DemoService.svc",
                          queryName: "GetItems"
                      }).refresh();

But when I deploy my services to the cloud, I get the following error:

XMLHttpRequest cannot load http://myapp.cloudapp.net/Services/RiaJS-Demo-DemoService.svc/json/GetItems. Origin http://localhost:51850 is not allowed by Access-Control-Allow-Origin.


Dec 19, 2011 at 12:19 PM

Problem solved by putting the customHeaders section into the web.config: (implemented code from above is not required)

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