Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Rest 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

The 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.

Hi 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

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

// 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

Thanks 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

Sorry, 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.

I 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!