coder and technology lover
Posts tagged ajax
goog.net.XhrIo: make simple ajax calls with Google Closure
Nov 17th
Closure has a consistent package called goog.net, which contains a lot of classes to work with ajax and remote http requests. In this post I want to show how to create a basic xhr object to make get/post calls, listen for related ajax events and send data to server. Once imported the main js file (base.js), we will require the following:
1 2 3 4 | goog.require("goog.dom"); goog.require("goog.net.XhrIo"); goog.require("goog.structs.Map"); goog.require("goog.Uri.QueryData"); |
The most important import is goog.net.XhrIo, which represents the object wrapping the XmlHttpRequest and allows us to retrieve and send information to/from the server. This class is a pretty low level api, which means that several tasks are not as simple and automatic as they would in jQuery or such libraries, but on the other hand this offers us more control and consciousness of what we are going to do.
The first step is instantiate an object of type goog.net.XhrIo and take a reference to it:
Jetpack: building Firefox plugin with Javascript (and jQuery)
May 29th
I discovered a couple of days ago a new open source project from Mozilla labs: Jetpack.
It’s an “API set” which allows to build Firefox’s plugins by using Javascript, HTML (including new HTML 5 tags: “video” and “audio“) and CSS (oh yes, in a single word Ajax).
Jetpack is still in an early development phase and released as 0.1 version.
Here you can find the (really brief) api documentation and here some examples.
I would like to try it in the future
Using jQuery to load an XML configuration file
Mar 11th
At work I’m integrating Viamichelin’s map api on a site and I realized a Javascript class which works as a proxy to Viamichelin’s services.
The api provide several methods to customize appearance of map’s controls and ui components (colors, dimensions, opacity and so on) but following the api’s documentation and examples this means to have several “hard coded” settings inside my code, which is an issue I want to avoid, so I thought to use an external XML configuration file and then call the methods using parametric values (which are retrieved by reading the file). Is pretty simple with ajax to load a text file, the only problem is that by default (or better, by nature) ajax is asynchronous and this can be an obstacle but fortunately with jQuery (but even with other libraries and personal XMLHttpRequest implementation) we can make synchronous calls by specifying it.
Handle multiple ajax calls in a fixed incremental order
Nov 10th
In a project which I’m working on, I’ve realized a Javascript object (I’ve called it MediaBrowser), which displays images and flv videos into a box (a dynamic div rendered at runtime). The data used by this object is contained into multiple XML files, which are loaded by ajax (potentially it can loads infinite files passed as array of urls). The problem was: ajax is by definition asynchronous, so each time I load the page with my cool MediaBrowser, the order in which the objects (images and videos) are processed and displayed varies based on the order in which responses to the ajax calls are returned. IE: if I call file_1.xml, file_2.xml and file_3.xml, which contain respectively:
Recent comments