Orkut Application Platform

On-Site Application Developer Guide: Remote Servers

Contents

Requesting Remote Data

Your application is served from our cache's domain, so, due to Javascript security restrictions on all major browsers, you would be unable to request data from servers located in other domains. Due to this restriction, our API supplies a method that allows you to request data that originates from a different domain.

To request data, you can use the gadgets.io.makeRequest function and indicate the remote URL and the content type (text or JSON) to fetch. You also supply a callback function that the framework will call when your data is loaded. Here is an example that loads the HTML source code for Google's search page into a text area:

<textarea id='mytextarea' style='width: 400px; height: 300px; overflow: scroll'>
Loading... please wait...</textarea>

<script type="text/javascript">
var MY_URL = "http://www.google.com";

function init() {
   var params = {};
   params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
   gadgets.io.makeRequest(MY_URL, callback, params);
   debug.say("Request sent. Waiting for reply...");
}

function callback(data) {
   debug.say("Reply received.");
   if (data.data && data.data.length)
      document.getElementById('mytextarea').value = 
          "google.com's page source:\n\n" + data.data;
}

gadgets.util.registerOnLoadHandler(init);

</script>

Using JSON

If you prefer to pull your data in JSON format rather than plain text, the API offers a convenient way to do this: all you have to do is specify gadgets.io.ContentType.JSON as your content type, and the remote JSON object will be automatically parsed and made available to you as a Javascript object.

For this example, you have to host this file somewhere:

[
  { "product": "Orkut",
    "company": "Google",
    "type"   : "Social Network"
  },
  { "product": "YouTube!",
    "company": "Google",
    "type"   : "Video Sharing Site"
  },
  { "product": "Google Spreadsheets",
    "company": "Google",
    "type"   : "Spreadsheet Web Application"
]

And here is an example code that pulls that file as a JSON object. We will pretend this file is hosted at http://example.com/test.json. You should replace this by the actual URL of the file if you intend to try out this example.

var MY_URL = "http://example.com/test.json";

function init() {
   var params = {};
   params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
   gadgets.io.makeRequest(MY_URL, callback, params);
   debug.say("Request sent. Waiting for reply...");
}

function callback(response) {
   var products = response.data;
   if (!products) {
      debug.say("ERROR.");
      return;
   }

   for (i in products)
      debug.say(products[i].company + "'s product "
              + products[i].product + " is a "
              + products[i].type);
}

gadgets.util.registerOnLoadHandler(init);

Notice how the JSON object is automatically parsed and becomes available to you in the callback() function as response.data.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.