Finding and using REST services

You can find a large number of REST services at the apigee.com site. Just scroll through the dropdown box under API.

You can also find REST interfaces for a number of other services. Almost any site that provides data will have a REST interface.

Once you have found a service you want to use, you need to figure out how to make the ajax call.

For this example, lets assume you want to display the xkcd comics. The following steps might be useful.

  1. First find the interface. I googled for "rest api xkcd" and found this documentation
  2. Then I created a small chunk of html to make sure I understood the interface.
  3. But it doesnt work. I get the error:
    XMLHttpRequest cannot load http://xkcd.com/info.0.json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.
    
  4. This occurs because the xkcd.com/info.0.json interface is not setting the "Access-Control-Allow-Origin:" field to allow web pages from other sites to access the REST interface (see the wikipedia article)
  5. So we will need to make a jsonp request to a service that does provide jsonp data.
  6. When I google for "rest api jsonp xkcd", I find that there is a jsonp server at "http://dynamic.xkcd.com/api-0/jsonp/comic". So I change my code to make a jsonp request in this chunk of html and it works.
  7. If you change the URL, but dont change the 'dataType : "json"' to 'dataType : "jsonp"', then you may get an error that mentiones an extra colon or something.
  8. I always bring up the chrome developer tools to inspect the object that is returned by a service so I will know what fields I can access.
Hopefully this gives you an insight into what to do to connect to a REST service.