Hi I have a WCF Rest service using ( WCF REST Service Template 40(CS)). I have managed to get it to return Json response. It is working fine when the project is run. But when I try to make a Ajaxcall to the service from the browser, I am getting Error:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at
http://192.168.0.70:8001/Service/test
. This can be fixed by moving the resource to the same domain or enabling CORS.
And The Ajax call:
$.ajax({
type: "POST",
url: "http://192.168.0.70:8001/Service/test",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {
var Tabels = msg.d;
alert('success');
}
});
Here is the web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ConString" connectionString="SERVER=localhost;DATABASE=fabapp;UID=root;PASSWORD=;"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
I tried adding crossDomainScriptAccessEnabled="true" but when I do that the service does not work on the localhost also. I get this:
Cross domain javascript callback is not supported in authenticated services.
Anything I need to change in web.config file?
localhost
domain). Is this answer yours or you cited the other answer by @Samir? His answer is 2 hours earlier and yours seems like a copy-paste of his plus some comments. – Haze