Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm creating a small web-site that needs to call a REST API to retrieve some JSON and display it. What is the best tool for this job?

There are so many Javascript libraries available now it is difficult to decide which one to choose. Is there are particular library that people would recommend that will do what I want both efficiently and without extensive development effort.

share|improve this question
    
questions about what technology is better are off-topic per site FAQ –  gnat May 4 '13 at 14:45
add comment

closed as not constructive by gnat, MichaelT, Martijn Pieters, maple_shaft May 5 '13 at 2:17

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

A word of warning: you normally can't call a REST API directly from Javascript!

It would be so simple:

Browser                 Your Webserver          Other Server
  |------- request HTML ------>|                     |    
  |<-- respond HTML+JS --------|                     |
  |                                                  |
  |------------------------- request --------------->|
  |<-------------------------- json -----------------|

But it isn't: Javascript loaded from your server cannot make AJAX calls to another server. This is called http://en.wikipedia.org/wiki/Same_origin_policy

You probably have to use your backend on your webserver to make the actual request:

Browser                      Your Webserver              Other Server
    |--------- request HTML ---->|                         |    
    |<-- respond HTML+JS --------|                         |
    |                            |                         |
    |------- request ----------->|  ------request--------->|
    |<-------- JSON -------------|<---- json --------------|

In this scenario the questions is: which libraries does you backend langauge offer for making HTTP requests?

Or, if you're lucky, the site is set up for http://en.wikipedia.org/wiki/Cross-origin_resource_sharing Then you really only need Javascript.

I'd recommend jQuerys "getJSON" http://api.jquery.com/jQuery.getJSON/

share|improve this answer
    
+1: It's trivial with JSON, provided you satisfy the security requirements. (In my case, that wasn't a problem at all as I was using it for the administration interface for my webapp.) –  Donal Fellows May 4 '13 at 20:02
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.