3

The below code works fine but the only problem is not giving me the textStatus. What do I need to do in order to get textStatus?

 var url = "http://host/MyServiceImpl.svc/GetCount?method=?";
        $.ajax({  
            dataType: 'jsonp',
            data: { Id: '1' },  
            jsonp: 'jsonp_callback',  
            url: url,  
            success: function (json, textStatus) {
                alert(json.d);
                alert(textStatus);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                debugger
            }
        });

or

$.getJSON(url, { Id: '1'},
  function (data, textStatus) {
         alert(textStatus);
});
Wayne Koorts
  • 10,861
  • 13
  • 46
  • 72
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • There isn't much offered for debugging, but the [jquery.jsonp plug-in](http://code.google.com/p/jquery-jsonp/) does offer `textStatus` ("error"/"success") on success, error, and complete for jsonp calls: [related SO question](http://stackoverflow.com/questions/1002367/jquery-ajax-jsonp-ignores-a-timeout-and-doesnt-fire-the-error-event). Note: `textStatus` is the second param on most of these handler functions. – patridge Sep 19 '11 at 18:26

1 Answers1

1

JsonP works by writing a tag to your document with the target Url as source. THe server then wraps it response in a function call.

somecallback( your data )  

The drawback of this is that XMLHttpRequest is not used and therefore is no "real" errordetection for jsonP. So basically it will not work because jsonP is a hack in itself.

That said there is a simple workaround that covers most cases where you need to signal a failure. Let the returned json have a .Success property and possibly a .ErrorMessage. I usually use a object of this structure:

{ Success: true|false, ErrorMessage: "", Data:json}   

ofcourse 404's or 500's will still cause a problem but the key is to try to avoid those when serving jsonP.

Mikael Eliasson
  • 5,157
  • 23
  • 27