11

I am trying to retrieve a JSON from this URL

http://www.iheartquotes.com/api/v1/random?format=json

via jQuery. I know the solution is JSONP, but since I have no control over the response text of the service or to wrap it in my own callback function, my aim is to somehow retrieve the response of the above URL using client-end scripts.

I have tried almost all the methods suggested from several answers from StackOverflow. These are the code blocks I have tried and the response's I've got.

1 . A direct call which returned the expected Access-Control-Allow-Origin error

$.getJSON("http://www.iheartquotes.com/api/v1/random?format=json",
   function(data) {
     alert(data);         
   });

Response:

XMLHttpRequest cannot load =1376682146029">http://www.iheartquotes.com/api/v1/random?format=json&=1376682146029. Origin http://stackoverflow.com is not allowed by Access-Control-Allow-Origin.

2 . The above code with the callback parameter added:

$.getJSON("http://www.iheartquotes.com/api/v1/random?format=json&callback=?",
   function(data) {
     alert(data);         
   });

Response:

Uncaught SyntaxError: Unexpected token :

Please note that when I click on the error, it takes me to the expected JSON response.

{"json_class":"Fortune","tags":["simpsons_homer"],"quote":"Holy Moly!  The bastard's rich!\n\n\t\t-- Homer Simpson\n\t\t   Oh Brother, Where Art Thou?","link":"http://iheartquotes.com/fortune/show/5501","source":"simpsons_homer"}

This is also expected as there is no callback function defined in the response.

3 . Through jQuery's Ajax method

$.ajax({ 
   type: "GET",
   dataType: "jsonp",
   url: "http://www.iheartquotes.com/api/v1/random?format=json",
   success: function(data){        
     alert(data);
   },
});

Response:

Uncaught SyntaxError: Unexpected token :

Adding the callback parameter to the above function doesn't change the response.

Any help or pointers from the experts to retrieve the JSON from the URL? I am testing this from the Chrome Dev Tools. I know I could call the service from the server-end code and then send it across to the client-end. But I want to see if this can be done through jQuery alone from the client-end.

EDIT: Based on Kevin B's comment: Got the expected output via YQL using jQuery's Ajax. But my question remains the same. Is there a native way to do it via jQuery as YQL is still a dependency?

// Using YQL and JSONP
$.ajax({
    url: "http://query.yahooapis.com/v1/public/yql",

    // the name of the callback parameter, as specified by the YQL service
    jsonp: "callback",

    // tell jQuery we're expecting JSONP
    dataType: "jsonp",

    // tell YQL what we want and that we want JSON
    data: {
        q: "select * from json where url=\"http://www.iheartquotes.com/api/v1/random?format=json\"",
        format: "json"
    },

    // work with the response
    success: function( response ) {
        console.log( response.query.results.json ); // server response
    }
});

This gives the expected response.

Ashok Goli
  • 5,043
  • 8
  • 38
  • 68

1 Answers1

1

This won't work in all browsers, but depending on which version of JQuery you're using try:

$.support.cors = true;

Obviously this also depends on the headers of the server response.

wild_nothing
  • 2,845
  • 1
  • 35
  • 47