|
GadgetsMakeRequest
Examples of how to use the gadgets.io.makeRequest functions
IntroductionIn 0.7, opensocial.makeRequest was changed to gadgets.io.makeRequest. The following samples show how to make various queries with the new syntax. Samples on this page use the output function syntax supplied by the CodeRunner gadget DetailsCachingCalls to makeRequest are cached. This wrapper function takes the same parameters as the makeRequest call, but accepts another parameter named refreshInterval which allows you to specify the cache duration. Specifying 0 will mean no caching. function makeCachedRequest(url, callback, params, refreshInterval) {
var ts = new Date().getTime();
var sep = "?";
if (refreshInterval && refreshInterval > 0) {
ts = Math.floor(ts / (refreshInterval * 1000));
}
if (url.indexOf("?") > -1) {
sep = "&";
}
url = [ url, sep, "nocache=", ts ].join("");
gadgets.io.makeRequest(url, callback, params);
}You can call this function in places where you would use gadgets.io.makeRequest(), just put the extra parameter at the end of the method arguments. ResponsesResponses come back as the following JavaScript object: {
data : <parsed data, if applicable>,
errors : <any errors that occurred>,
text : <raw text of the response>
}
Normal Requestsfunction makeNormalRequest() {
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
var url = "http://graargh.returnstrue.com/buh/makerequest.php";
gadgets.io.makeRequest(url, response, params);
};
function response(obj) {
//obj.text contains the text of the page that was requested
output(obj.text);
};
makeNormalRequest();DOM Requestsfunction makeDOMRequest() {
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.DOM;
var url = "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml";
gadgets.io.makeRequest(url, response, params);
};
function response(obj) {
//obj.data contains a Document DOM element corresponding to the page that was requested
output(obj.data);
};
makeDOMRequest();JSON Requestsfunction makeJSONRequest() {
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
var url = "http://graargh.returnstrue.com/buh/fetchme.php";
gadgets.io.makeRequest(url, response, params);
};
function response(obj) {
//obj.data contains a JavaScript object corresponding to the data that was requested
output(obj.data);
};
makeJSONRequest();Feed RequestsCurrently does not work on Orkut function makeFeedRequest() {
var params = {};
params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.FEED;
var url = "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml";
gadgets.io.makeRequest(url, response, params);
};
function response(obj) {
//obj.data contains a JavaScript object corresponding to the feed that was requested
output(obj.data);
};
makeFeedRequest();POST Requestsfunction makePOSTRequest() {
var params = {};
var postdata = {
data1 : "test",
data2 : 1234566
};
params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);
var url = "http://graargh.returnstrue.com/buh/makerequest.php";
gadgets.io.makeRequest(url, response, params);
};
function response(obj) {
output(obj.text);
};
makePOSTRequest();Signed Requestsfunction makeSignedRequest() {
var params = {};
params[gadgets.io.RequestParameters.AUTHORIZATION] = gadgets.io.AuthorizationType.SIGNED;
var url = "http://graargh.returnstrue.com/buh/fetchme.php";
gadgets.io.makeRequest(url, response, params);
};
function response(obj) {
//obj.text contains the text of the response
output(obj.text);
};
makeSignedRequest();
|
Sign in to add a comment
Great examples and codes ! Thanks
The above example under feed request is returning "null" value... is this something wrong with the code or else its the problem from Gadget API. I have tried the above code with all possible feeds, even though its returning same "null" value for all the feed URLs
makeDOMRequest() works very well. Is there any code snippet that shows how to parse the XML using javascript to display all titles?
I see the answer at http://code.google.com/apis/opensocial/articles/makerequest.html; I see conflicting statements about content-type. Some articles recommend using content-type="url" for commercial gadgets. http://code.google.com/apis/gadgets/docs/publish.html#PageCreator recommends to use content-type="html" to optimize for heavy traffic.
When using makeRequest the response cotain the result and the following error message.
That isn't an error message, and you should be able to ignore it.
If you're fetching plain text/HTML, you can use the text property of the object passed into the callback:
function callback(resp) {
}If you're fetching JSON or XML from your server, use the data property instead:
function callback(resp) {
}uguyh
That's great, but why does no one, and I mean no one, ever, ever, ever talk about the server side? I'm getting a request as a GET even though I set it to POST and I can't access the post variables (like test/123456 above) either. Help!
If you are having any problems using makeRequest with a particular container (e.g. orkut, iGoogle, etc.), please post in that container's discussion group. POST requests should definitely be supported by all networks.
i tried to fetch a plain html for server but, i think that orkut environment does not accept. Does anyone know about that??