Making Standard Data Requests
Unless you’re building a site that requires a secure data transfer, you’ll access
the majority of your data through standard RESTful server requests. You
can make these standard requests through osapi.http.method,
where method is get, put, post,
or delete.
Since we are using the osapi JavaScript functionality, we will
need to add a Require statement to
make the request methods available for us to use. Once the methods are
available, we can use them to build out a functional example to capture
data from an external source and display the content within the
application.
The following example will make a GET request to Flickr via the Yahoo! Query Language (YQL) to return photos that match the search term “Montreal.”
Those results are then parsed, and <img> tags are
generated based on the results of each photo and displayed within the
application:
<?xml version="1.0" encoding="utf-8"?> <Module> <ModulePrefs title="GET Request to Flickr via YQL"> <Require feature="osapi"/> </ModulePrefs> <Content type="html" view="canvas"> <![CDATA[ <div id="imgContainer"></div> <script type="text/javascript"> //GET request callback function requestCallback(response){ var photolist = response.content.query.results.photo, html = ""; //loop through each image and create an <img> tag for (var i in photolist){ if (photolist.hasOwnProperty(i)){ html += "<img src='http://farm" + photolist[i].farm + ".static.flickr.com/" + photolist[i].server + "/" + photolist[i].id + "_" + photolist[i].secret ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access