Help with Random quote machine, $.ajax is not a function

Help with Random quote machine, $.ajax is not a function
0

#1

hey guys, I’ve been 2 days with this challenge but I’m stuck, it shows me this error:
Uncaught TypeError: $.ajax is not a function
I don’t know what else I should do, this my code:

$(document).ready(function(){
$.ajax({
type: 'GET',
url: 'http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en',
success: function(data){
console.log(‘success’.data);
}
});
});

I hope you guys may help me, cheers.


#2

Are you sure you have added jQuery in your project? Maybe you can share your codepen if you have one.


#3

It looks like you have jQuery added, otherwise it would have thrown an error at the first instance of “$”. Is that the only error that comes up? I do notice a syntax error though in the console log. You’ve added a period when it looks like you meant to have added a colon. Also it needs to be inside of the string.


#4

This is my codepen and it’s the only error that shows.


#5

It looks like .ajax is not included in the slim version of jQuery. Try again with the full version: https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js.

Then you’ll see this warning in the console:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)

You’ll probably have to use a proxy to deal with this, or use a different API (this works btw: https://cors-anywhere.herokuapp.com/https://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en)


#6

@kevcomedia thanks for your help, it was the jQuery version, But why I must use a proxy? why don’t it work without it?


#7

If I understood it correctly, it’s because the JSON response is coming from a different domain (which is not allowed by default). If the response has Access-Control-Allow-Origin set to * in its header, you can use the URL as it is.

Someone wrote a detailed explanation in the forum long ago: