Making AJAX and External Data Requests

During normal program flow, you may need to modify your application or augment the server data sources, such as a database, with new content. To meet this need, OpenSocial makes available an http request method through the standard JavaScript library.

Developers can use this method to make RESTful requests (GET, PUT, POST, DELETE) between the application and the server to alter their state without impacting the user experience.

The methods you can use to make these requests are all under the osapi.http object and include:

  • osapi.http.get(url, params)

  • osapi.http.put(url, params)

  • osapi.http.post(url, params)

  • osapi.http.delete(url, params)

In addition to the URL to make the http request to, there are a number of parameters you can introduce within these request calls. They are listed in Table 6-2.

Table 6-2. http request parameters

Parameter

Description

authz (string)

The authorization method to use when sending data to the server. This value may be none (default), signed, or oauth.

body (string)

For PUT and POST requests. The data to be sent to the server from the request.

format (string)

The format of the data returned. This value may be json (default) or text.

headers (string/array of strings)

Optional headers to send with the data request.

oauth_service_name (string)

The service element in the gadget spec to use for the request. Default is an empty string (" ").

oauth_token_name (string)

The OAuth token to use in the request. Default is an empty string (" ").

oauth_request_token ...

Get Programming Social Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.