1

I'm trying to use Goodreads APIs for my sideproject but facing some problem. The below URL returns XML data. If I use datatype: 'xml' then I get cross-domain error and thats why I've used 'jsonp' but still gets Uncaught SyntaxError: Unexpected token < error. Am I doing wrong?

$.ajax({
  url : 'http://www.goodreads.com/user/show/userid.xml?key=developerKey&id=userId, 
  method : 'get', 
  dataType : 'jsonp', 
  success : function (data) {
    console.log($.parseXML(data));
  }
});
  • Here's [one possible solution](http://weedygarden.net/2011/01/consuming-remote-xml-as-jsonp/). – Michael Mior May 01 '12 at 16:19
  • @MichaelMior : No luck. "Origin null is not allowed by Access-Control-Allow-Origin." –  May 01 '12 at 16:42
  • That can't happen because the proxy script would be running on your on domain. In that case you would just make a regular JSON request to the same domain. – Michael Mior May 01 '12 at 18:32
  • I'm running the above code locally and the url returns xml data. Upon making a regular JSON/text/xml request, I get "cross-domain" error. –  May 01 '12 at 18:59
  • This would suggest that you can't make any AJAX requests to your own domain. In which case, something strange is going on. – Michael Mior May 01 '12 at 19:41

3 Answers3

6

Use an external proxy that supports CORS to make the goodreads request.

At least Yahoo YQL can be used as a proxy, as done in this example code (it's free, no API key needed!). YQL returns the Goodreads XML response wrapped inside /query/results.

var url = "http://www.goodreads.com/user/show/userid.xml" +
          "?key=developerKey&id=userId";

$.get("http://query.yahooapis.com/v1/public/yql",
    {
        q: "select * from xml where url=\""+url+"\"",
        format: "xml"
    },
    function(xml){
        // contains XML with the following structure:
        // <query>
        //   <results>
        //     <GoodreadsResponse>
        //        ...
        console.log(xml);
    }
);

More about YQL in this answer: https://stackoverflow.com/a/8579158/1068385

Community
  • 1
  • 1
juhoautio
  • 1,541
  • 1
  • 22
  • 23
0

Trying using 'application/xml' instead.

Jaimal Chohan
  • 8,530
  • 6
  • 43
  • 64
  • No luck. "Origin null is not allowed by Access-Control-Allow-Origin." –  May 01 '12 at 15:22
-1

(1) The cross-domain error message suggests that your app isn't correctly authenticated to Goodreads.

url : 'http://www.goodreads.com/user/show/userid.xml?key=developerKey&id=userId, 

is missing its closing quote and doesn't evaluate the variables for the query string. I take it you mean

url : 'http://www.goodreads.com/user/show/userid.xml?key=' + developerKey + '&id=' + userId,

(2) It looks like you are getting a response from the Goodreads server, but you also might want to try this to make sure you are getting something that you expect:

dataType: 'text'

and just console.log() the data without parsing it.

David Gorsline
  • 4,933
  • 12
  • 31
  • 36
  • No luck. "Origin null is not allowed by Access-Control-Allow-Origin." –  May 01 '12 at 16:42