Ajax JavaScript CSS HTML 5 Firefox

XDomainRequest object for cross-sites Ajax

The XDomainRequest object is not standard. It has been created by Microsoft and implemented in Internet Explorer 8 and 9 for exchanging data between domains, just as the XMLHttpRequest object exchanges data between the browser and the server on a single domain.

Here is the current specification of the XDomainRequest object.

Attributes

contentType

This property contains the type of document. When you send data, you have to assigns the property according to the document that is sent. When you load a document, this property contains the type of file which was loaded.

xdr = new XDomainRequest(); 
xdr.contentType = "text/plain";

responseText

Contains the contents of a file you just load. We must wait until the onload event happened to read the contents of this variable.

alert(xdr.responseText);

timeout

Contains the maximum time allocated to complete the request. (Probably in milliseconds, this is not specified).

xdr.timeout = 1200:

Events

onerror

Triggered when the action can not succeed. A function is assigned to the event to be executed when it is raised.

function problem()
{
    alert("Error in the request");
}
xdr.onerror = problem;

onload

Triggered when loading is completed. To use it, assigns a function to it and this function reads the content of the attribute responseText, which when it is fully assigned causes the event triggered.

function loading()
{
    alert(xdr.responseText);
}
xdr.onload = loading;   

onprogress

Fired at intervals during the loading of a document on the remote site. Created to display a progress bar or inform on the progress of the request in any way.

var x = 0;
function progress()
{

     bar.setState(x++);
}
xdr.onprogress = progress;

In this example, it is assumed that bar is a JavaScript object that displays the progress. We can base the remaining time on the content of responseText because it is partially assigned during loading (and one can know the size of the file to load elsewhere).

ontimeout

Triggered when the time limit for loading (defined by the attribute timeout) is exceeded before the end of loading, and thus before the outbreak of the onload event.

Methods of XDomainRequest

open

Open a connection with the server by defining a method and a file.
The method can be one of the HTTP method that follow:

GET to load a document.

POST to send data to the server.

xdr.open(method, url); 

send

Send data or launch a request to load a file. With POST, contains data to send as parameter, in GET mode has no parameter or the empty string "".

xdr.send([data]); 

abort

This method can be called during the execution of the request to cancel it. It may is associated to a button of the interface to allow users to cancel the operation.

xdr.abort(); 

Examples

Complete code for a GET request with XDomainRequest.

xdr = new XDomainRequest(); 

xdr.onload=function()
{
    alert(xdr.responseText);
}
xdr.open("GET", "http://www.anotheresite.com/myfile.txt");
xdr.send(); 

Complete code for a POST request.

xdr = new XDomainRequest(); 

xdr.onload=function()
{
    alert("completed");
}
xdr.open("POST", "http://www.anothersite.com/script.php");
xdr.send("données à envoyer"); 

Conclusion

There is no major difference between the interfaces of XDomainRequest and that of its predecessor, the XMLHttpRequest object (originally created by Microsoft as well). It should be noted, however, two useful additions: ontimeout and onprogress.

Another difference too, the onload event which replaces onreadystatechange and readyState and in fact simplifies the use of the object.
However onload and onprogress are planned for the Level 2 of the specification of XHR.

You should not use this object for a public usage at this time. On the one hand, the definition is not complete, on the other hand, it only works on Internet Explorer 8 and 9 and is replaced by XHR 2 on IE 10. You can use a substitute as described in the article Cross-site Ajax.

More information


© 2008-2013 Xul.fr