Archive for June 4, 2007

JavaScript XMLHttpRequest open Method: Calling a URL in Ajax

Posted in JavaScript, Software, Web, internet, programming on June 4, 2007 by Joey

In JavaScript, to call a URL for usage in an Ajax application, an XMLHttpRequest object must be created. This was described in a previous post:

JavaScript: Creating XMLHttpRequest object for Ajax

Once the XMLHttpRequest (XHR) object is created, a URL can be called by using the XHR’s open method, as follows:

objXHR.open('GET','http://myURL.com',true);

The three parameters passed to the open method are as follows:

1: The method by which the HTTP should be retrieved: GET or POST
2: The requested URL. A relative or absolute URL may be used here. If an absolute URL is being used, most browsers implement a same-origin policy, meaning the URL being requested must be on the same domain as the domain from which the URL is being requested.
3: How the request should be issued: True for asynchronous; False for synchronous.

After the XHR object has opened a request, send the request.