Tuesday, October 15, 2013

Cross Domain JSONP getJSON Issue Callback not firing

I am working on a Windows 8 Mobile App which uses Rest Service calls to get Data. My application was working still yesterday , but after I did some config changes it stopped working. I tried to trace this issue using fire bug. surprisingly I am getting data from the service and browser showing response as JSON string. The call back is not firing, its looking very strange to me. Below is the sample code.



function getRecentProjectMOMs() {
        $.mobile.showPageLoadingMsg();
        $.getJSON(hostAddres + "mobileservice.svc/GetRecentProjectNotes?callback=?").done(function (data) {
           //the below functionality is not executing
           alert('hi');
           $.each(data, function (i, item) {
                appendItem(item);
            });
            $.mobile.hidePageLoadingMsg();
        });
    }

After a couple of minutes R&D , I came to know that  some how I screwed up the config file while enabling https. As we know,  we use JSONP to avoid crossdomain scripting errors,Its there before but, I have removed the below attribute from the binding element in webHttp binding by mistake.



crossDomainScriptAccessEnabled="true" ,

After  adding this to my binding element then call back function started working. At the end the binding element should look like the below lines of code.

<webHttpBinding  >
<binding name="webHttpBindingJsonP"  crossDomainScriptAccessEnabled="true" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
        </binding>
</webHttpBinding  >
 

I am blogging my experience to help others who are fighting for hours to find the root cause. This type of silly mistakes will cause so much of irritation and waste of time.