0

Here's my code based off this question

$(document).ready(function(){
  $('#new-quote').click(function(){
    var url = 'http://api.forismatic.com/api/1.0/?format=jsonp&jsonp=_';
    $.getJSON(url, function(data){
      console.log(data);
    });
  });
});

This get's me the error:

XMLHttpRequest cannot load http://api.forismatic.com/api/1.0/?format=jsonp&jsonp=. Redirect from 'http://api.forismatic.com/api/1.0/?format=jsonp&jsonp=' to 'http://forismatic.com/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

I've also tried the code:

$(document).ready(function(){
  $('#new-quote').click(function(){
    var url = 'http://api.forismatic.com/api/1.0/?format=jsonp&jsonp=_';
    $.getJSON(url).done(update).fail(err);
    function update(response){
      console.log(response);
    }
    function err(jqxhr, textStatus, err){
      console.log('Request failed');
    }  
  });
});

And it gives me the same error. Along with the 'Request failed message'. I'm using jQuery 3.1.1. What am I missing here? Sorry if this is a duplicate but I've read the questions here and haven't found the answer

This question asks about the underlying concept of Access-Control-Allow-Origin. The guy also literally states that he doesn't want to use jsonp. I want use jsonp format with $.getJSON format and am not sure why it's not working.

Community
  • 1
  • 1
avatarhzh
  • 2,175
  • 4
  • 21
  • 32
  • 1
    http://stackoverflow.com/questions/3245824/getjson-to-get-jsonp-data – Klemen Tusar Dec 20 '16 at 12:37
  • Possible duplicate of [How does Access-Control-Allow-Origin header work?](http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work) – rckrd Dec 20 '16 at 12:37
  • @techouse that question deals with $.getJSON from a local json file. – avatarhzh Dec 20 '16 at 17:53
  • @rckrd at the end of the question the person says he doesn't want to utilize jsonp – avatarhzh Dec 20 '16 at 17:54
  • 2
    Does the service actually support JSONP? getJSON will not be JSONP without setting the callback – epascarello Dec 20 '16 at 18:12
  • Possible duplicate of [Make cross-domain ajax JSONP request with jQuery](https://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery) – Liam Jul 19 '19 at 13:09
  • Typo: The callback placeholder character for jQuery is `?` not `_` – Quentin Mar 19 '21 at 16:10

2 Answers2

0

Found the answer, I had to tweak the url to http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=? and it worked

avatarhzh
  • 2,175
  • 4
  • 21
  • 32
-1

In jQuery.ajax you have a setting named crossDomain. By default is false to avoid CORS, set it to true to enable CORS calls with ajax.

More Information : http://api.jquery.com/jquery.ajax/

OrcusZ
  • 3,555
  • 2
  • 31
  • 48
  • That just forces crossdomain behavior for a same origin request. The URL isn't the same origin so that will have no effect. – Quentin Mar 19 '21 at 16:09