Tuesday, 19 November 2013

Calling handler file (.ashx) in cross domain.

From last couple of days i was facing a problem in calling hadler file in cross domain.
when i write a code it works fine in the same domain but when i call the same in cross
domain it was not working. I was facing parseError problem there. After a lot much googling i found a sollution here.

In theory whenever you call from the javascript ajax, the url adds "callback" parameter with
its every call and its generated by jQuery. Whenever the response return, the value of callback
parameter must be there with the response to view data on page. It is like handsaking.

reference from : http://www.dotnetbull.com/2013/08/parsererror-jquery-ajax-request-jsonp.html

Your javascript ajax call :

                type: "POST",
                url: 'http://www.abcd.com/xyz.ashx?term=test,
                async: true,
                cache: false,
                dataType: "jsonp",
                success: function (data) {
// your code here
                error: function (response, textStatus, errorThrown) {

whenever you call from ajax it adds 'callback' parameter to the url you are calling.


so to get the result it need to be return with your response in your code.
so to achieve that you just need to append below in your handler code.

Your handler code :

1. add a parameter in your code like:

string callback = context.Request.QueryString["callback"];

2. Then after whenever you return just add this with your response like:

System.Web.Script.Serialization.JavaScriptSerializer javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    string serEmployee = "";
                    context.Response.ContentType = "text/html";

Thats it done!

Enjoy coding....