I am having problem with my Jquery+ajax call that will consume one of my web service method via cross domain. i have been trying all the possible way to accomplish but still no success. please help me with what i am doing wrong. may be i need to configure web server for some security settings? below is my code. please let me know if you have any question regarding with my code.
I added this in web.config of my web service.
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
and this to my application
$(document).ready(function() {
$.support.cors = true;
$.ajax({
url:'http://si-cb01:10000/service1.asmx/GetJsonData',
type: 'GET',
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: "json", // change data type to jsonp
success: function (response) {
alert(response.d);
Result = response.d;
},
error: function (response) {
alert("Error");
}
});
});
IE Console showed this error
XMLHttpRequest: Network Error 0x80070005, Access is denied.
Google Chrome Console showed this error .
XMLHttpRequest cannot load http://si-cb01:10000/service1.asmx/GetJsonData. Here si-cb01 is nothing but system name with IP 192.168.*.***
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:xxx' is therefore not allowed access.
The response had HTTP status code 500.
I looked up the problem and it seems to be a Missing Cross-Origin Resource Sharing (CORS),but I cannot understand the solution for this.
CORSpolicy also applies when the ports differ, even if the domain is the same, which means callinghttp://domain:123/fromhttp://domain:789/also requires theCORSto be set.web.configof the server application, no the client application. UsingJSONPwill not work by default, as I explained in my answer.