Sunday 15 May 2011

json - jQuery ajax jsonp calls error after success -


I'm using jQuery 1.9.1 which is doing JSONP against ESSI API. There is a normal routine below to call in the API; It accepts a signed request (URL), a JSNP callback name and an error handler. ($ Url: signOAuthRequest, type: "GET", data type: "jsonp", jsonpCallback: callback, jsonp: (

Usage:

  call ("http: //the.api/? Signrequest", "myCallback", myErrorHandler);   

And it works myCallback Fire, getting Jason object looks good Everything is OK.

In the error handler, the status code is 200 and the error message is "parsererror" . The error "myClall was not called" The thing is that, myCallback was called definitely and it was The JSN object passed was properly parsed, so I did not find it at all.

Why is this error being thrown on an otherwise successful call?

Based on the comments and what is in your question, I will write that code: / P>

  function (signed oathurum, callback name, successfanes, error) {$. Axx ({url: Signed Ohah Request, Type: "Received", Data Type: "JSONP", jsonpCallback: callbackName, jsonp: false, cache: true, success: successFn, ​​error: function (jqXHR, textStatus, errorThrown ) {If (typeof errorFn == 'undefined') {errorFn (jqXHR, textStatus, errorThrown);}}}); }   

I made the following changes:

An additional parameter was added to pass a successful callback. This is necessary because jQuery will define callback for you so that it receives a successful response and it can pars.

Extracted crossdomain:. True because it is unnecessary

removed proccessData: false because it does not affect jsonp requests and you added any data

Success has not passed: successFn so that jQuery successFn will execute with pars on success with json data.

CallbackName is renamed Callback so that it is more clear about what it does.

If you make an alternative solution, you still insist on defining callback, you have to use the script datatype (taken from the comment below)

  $ Ajax ({Url: signedOAuthRequest, datatype: "script", cache: true});    

No comments:

Post a Comment