Fork me on GitHub Fork me on GitHub

XMLHTTPRequest


<%  	
 var url = 'http://jaggeryjs.org/apidocs/resources/home.jag'; 
 var xhr = new XMLHttpRequest();
 xhr.open("GET", url);
 xhr.setRequestHeader("user" , "madhuka");
 xhr.send();
 
 print("ReadyState : "+xhr.readyState);
 print("<br>ResponseStatus : "+xhr.status);
 print("<br>ResponseText : "+xhr.responseText);
%>

Overview

XMLHttpRequest provides the functionality of server side XML HTTP communication. This follows the W3C XMLHttpRequest specification.


Members

Member Type Description
onreadystatechangeFunctionReady state change callback when async mode is used.

Operations

Operation Returns Description
XMLHttpRequest()None
var xhr = new XMLHttpRequest();
Returns a reference to the constructor of an object.
statusStringRetrieves the HTTP status code of the request.
readyStateString

Retrieves the friendly HTTP status of the request. Holds the status of the XMLHttprequest. Changes from 0 to 4

  • 0: xhr request not initialized
  • 1: server connection established
  • 2: request received
  • 3: processing request
  • 4:request finished and response is ready
statusTextStringReturns the status-text (e.g. "Not Found" or "OK")
responseTextStringRetrieves the response body as a String.
responseXMLXMLRetrieves the response body as an E4X XML Object
open(method,url,async)String
xhr.open(method,url,async);

Specifies the type of request, the URL, whether the request should be handled asynchronously or not, and other optional attributes of a request.

  • method: the type of request: GET or POST
  • url: the location of the file on the server
  • async: true (asynchronous) or false (synchronous)
send(String)String
xhr.send(String);
Sends the request off to the server. String is only used for POSTing a requests
getAllResponseHeaders()String
xhr.getAllResponseHeaders();
Returns header information.
setRequestHeader(name, value)None
xhr.setRequestHeader("Content-Type", "application/json");
Adds an HTTP header to the request.
getResponseHeader(name)String
xhr.getResponseHeader("Content-Type");
Gets the specified HTTP header from the response.
abort()None
xhr.abort();
Abort the request if async mode is used.