i want to Enable Web service to be accessed by any domain so i researched end up with following thing
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
crossdomain.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<?xml version="1.0" ?>
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="SOAPAction,Content-Type"/>
<allow-access-from domain="*" />
</cross-domain-policy>
In my web.config I have added Following thing
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
i have used Both file one by one and used both file at same time. still not able to request from Other domain.
Here is the jquery Code
var cid="My String Input";
var webMethod = "MyWemMethodUrl";
var parameters = "{'ContactID':'" + cid + "'}";
$.ajax({
type: "POST",
url: webMethod,
data: parameters,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
debugger;
var _JSONObj = jQuery.parseJSON(result.d);
if (_JSONObj.StatusCode == "0") {
alert("Error");
}
else {
alert("Success");
}
},
error: function (jqXHR, exception, thrownError) {
debugger;
if (jqXHR.status === 0) {
alert('Not connected.\nPlease verify your network connection.');
} else if (jqXHR.status == 404) {
alert('The requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
it always goes jqXHR.status=0 path in error
even i tried dataType: "jsonp" instread of dataType: "json".
here is my console screen From Chrome browser
Okay finally I found what was wrong with my code.
i add callback in url in javascript Ajax call and set datatype value to jsonp
$.ajax({
type: "POST",
url: WebMethod+'?callback=jsonCallback',
crossDomain: true,
contentType: "application/json; charset=utf-8",
data: parameters,
dataType: "jsonp",
jsonpCallback: 'jsonCallback',
success: function (result) {
/*SUCCESS CODE*/
},
error: function (jqXHR, exception, thrownError) {
/*error Code*/
}
});
And instead of string return type function , i changed to non return type with void.
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void WebMethod()
{
String ReponseResult = "";/*Json String that i want to return*/
/*My Code and logic
----
*/
/*To return Json String I added following Code*/
try
{
string callback = HttpContext.Current.Request.Params["callback"];
string json = ReponseResult;
string response1 = string.IsNullOrEmpty(callback) ? json : string.Format("{0}({1});", callback, json);
// Response
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.Write(response1);
}
catch (Exception ex) { }
}