-2

I make an ajax request:

$.ajax({
    url: "http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en&jsoncallback=?",
    dataType: 'json'
});

and get an error:

SyntaxError: Unexpected token :

When I click on that error in dev tools I see such an object which looks pretty ok for me:

{
    "quoteText":"Reality leaves a lot to the imagination. ",
    "quoteAuthor":"John Lennon",
    "senderName":"",
    "senderLink":"", 
    "quoteLink":"http://forismatic.com/en/9dceb1ebf1/"
}

Where is the problem? I'm unable to find the solution.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • The URL you've provided is **not** a JSONP service yet you're trying to use it as one (via the `jsoncallback=?` parameter). It is also not set up for cross-origin requests so you'll have to use a proxy. – Phil Aug 28 '16 at 23:09
  • 1
    Possible duplicate of [Loading cross domain html page with AJAX](http://stackoverflow.com/questions/15005500/loading-cross-domain-html-page-with-ajax) – Phil Aug 28 '16 at 23:10

1 Answers1

0

Putting a =? in a URL overrides the dataType and tells jQuery to make a JSONP request instead of an XHR request.

The URL you are requesting returns JSON, not JSONP, so you get an error.

Remove &jsoncallback=? from the URL.

You will then get this error:

XMLHttpRequest cannot load http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en&js. No 'Access-Control-Allow-Origin' header is present on the requested resource.

… which is explained in this question.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335