Tito
Tito

Reputation: 9044

jquery not properly handling the jsonp data from twitter api call

See the below code,I am trying to handle the error which is returned by the twitter api call. Remember Jquery do not handle jsonp datatypes and hence the timeout , the below code will obviously throw an error for a non existent twitter ID. I want to catch that error in my req.error method and show it to the user. But apparently , the error is hidden and does come to console.log('Oh noes!'+msg.error); This has surely something to do with the jquery handling jsonp type data. Has anyone encountered the same ? Any solutions ?

  function findUserInfo(){


    var req = $.ajax({

    url: "https://twitter.com/users/show.json?id=neverexistID",
    dataType : "jsonp",
    timeout : 10000

    });

    req.success(function(msg) {
        console.log('Yes! Success!'+msg);

    });

    req.error(function(msg) {
        console.log('Oh noes!'+msg.error);
    });


            }   

Answer: jsonp calls are special and the errors thrown is usually hidden,and thats why I couldn't handle the error situation,the below plugin handles the situation well and solved my issue.

jsonp plugincode.google.com/p/jquery-jsonp

Upvotes: 1

Views: 346

Answers (1)

Cesar Canassa
Cesar Canassa

Reputation: 20163

There is a workaround for your problem, change your url call to:

url: "https://twitter.com/users/show.json?suppress_response_codes&id=neverexistID",

From the Twitter documentation

suppress_response_codes: If this parameter is present, all responses will be returned with a 200 OK status code - even errors. This parameter exists to accommodate Flash and JavaScript applications running in browsers that intercept all non-200 responses. If used, it’s then the job of the client to determine error states by parsing the response body. Use with caution, as those error messages may change.

Upvotes: 2

Related Questions