Hi,
I’m doing the “Build a Random Quote Machine” task. I use a codepen.io as my editor.
I’m trying to retrieve data from http://quotes.stormconsultancy.co.uk/random.json using jQuery getJSON() method.
But the following error happenes: XMLHttpRequest cannot load http://quotes.stormconsultancy.co.uk/random.json . No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://s.codepen.io ’ is therefore not allowed access.
Could you help please. Thanks in advance.
1 Like
Use this better http://forismatic.com/en/api/
and the query should have jsonp=?
@tukhtarov do a quick Google search on something called “CORS”, it will explain your issue and solutions to it. For solution think JsonP…
1 Like
After a lot of searching I figured out that I have to find APIs that offer either JsonP or CORS support. Am I thinking in the right way?
You’re almost there already, you posted that you are already using jQuery getJSON() so to get around the CORS issue just use getJSON() in JSONP mode. It involves adding a callack function.
1 Like
I tried to do this:
$.getJSON( "http://api.forismatic.com/api/1.0/method=getQuote&format=json&key=457653&lang=en&jsonp=?" ,
function(json) { console.log(json); }
);
And I faced an “Uncaught SyntaxError: Unexpected identifier” error.
I see now my mistake. I made format=json when I should make format=jsonp.
Great thanks, guys!
I appreciate your help so much!
MK-61
May 19, 2016, 3:04pm
10
Hi,
I tried to check my code today. It doesn’t work again.
I see the following in the console:
Mixed Content: The page at ‘https://codepen.io/tukhtarov/pen/jqjbVP?editors=1111 ’ was loaded over HTTPS, but requested an insecure script ‘http://api.forismatic.com/api/1.0/?method=getQuote&format=jsonp&lang=en&jsonp=jQuery222038259937173673575_1463839667594&_=1463839667595 ’. This request has been blocked; the content must be served over HTTPS.
It appears that the error message in console is telling you that codepen is served as secure https and that forismatic is being served as unsecure http content. Mixed Content is described by MDN.
Try changing your quote server to something served securely like theysaidso .