Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialColin Starger
869 PointsRest API Get Calls in Javascript?
I am just getting back into coding after a 15 year hiatus (used to be a unix/C++ coder). I am now a lawyer and my motivation is to work with a Rest API put out by Court Listener (see https://www.courtlistener.com/api/rest-info/). I am working my way through the JavaScript tutorials.
My question is, what is the best way to learn how to make GET Requests to a Rest API in Javascript? Do I first have to learn about Django Querysets? I am really just trying to figure out what to read/watch first, then second, then etc.
Thanks!
6 Answers
Alim Hasanov
Courses Plus Student 4,081 PointsThe easiest way I know and that I have used is with Jquery like:
$.get("www.courtlistener.com/api/rest/v1/opinion/")
.success( function( returned_data ){
// Here You can display the returned information
// In order to just see if it works you can " console.log(returned_data) " to see it the browser's console
} );
// P.S. Hope It Helps.
Colin Starger
869 PointsThanks Alim. This was helpful indeed. (First I had to figure out what Jquery was; then download the lib. Great stuff).
However, I am now experiencing a new problem. I think it is with authentication on the Court Listener site. The error I get on the console is "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."
I have a username & password, but can't send it along using the syntax I would directly in the browser. The Court Listener API page recommends sending a cURL. Is there a JavaScript library that can do basic HTTP authentication?
Thanks again for your help! Colin
Alim Hasanov
Courses Plus Student 4,081 Points// Before the $.get().... try this
$.ajaxSetup({
headers: {
'Authorization': "Basic " + btoa(USERNAME + ":" + PASSWORD)
}
});
$.get("www.courtlistener.com/api/rest/v1/opinion/")
.success( function( returned_data ){
// Here You can display the returned information
// In order to just see if it works you can " console.log(returned_data) " to see it the browser's console
} );
// USERNAME is your "username" and same for PASSWORD
Colin Starger
869 PointsThanks again! Unfortunately, I'm still getting errors. See below for the console dump. At this point, I may try to reach out the the Court Listener folks directly. You've actually helped me a lot by hipping me out to Jquery. I suspect the key is somewhere in ajax...
** The error **
OPTIONS https://www.courtlistener.com/api/rest/v1/opinion/ No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. jquery.js:8706 XMLHttpRequest cannot load https://www.courtlistener.com/api/rest/v1/opinion/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. CourtListener_Test.html:1
Alim Hasanov
Courses Plus Student 4,081 PointsSorry, I haven't used ajaxSetup before but I think this is the general way to do it. You have to tweak Jquery and ajax.. You shouldn't need any other library for that. Side Note: The error is most probably due to the Cross Domain Request or Cross Site Request!
You can try also to use this small parameter in your request. It allows cross site requests.
xhrFields:
'withCredentials': true
it is probably one or two rows of code that we are missing.
Colin Starger
869 PointsI am very confident that, as you say, it's just a couple of row of code that I need. The good news is everything can be done with Jquery and ajax. I'll keep studying and playing around. Once again, I really appreciate your help!
Colin Starger
869 PointsColin Starger
869 PointsHi Alim -- I just posted this comment below on my own page. Oops. Not sure if you would be alerted; so I'm re-posting directly to your comment.
Thanks Alim. This was helpful indeed. (First I had to figure out what Jquery was; then download the lib. Great stuff).
However, I am now experiencing a new problem. I think it is with authentication on the Court Listener site. The error I get on the console is "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."
I have a username & password, but can't send it along using the syntax I would directly in the browser. The Court Listener API page recommends sending a cURL. Is there a JavaScript library that can do basic HTTP authentication?
Thanks again for your help! Colin