Just a few notes to make on this...
I am new to this whole AJAX thing, so forgive me if i am pointing out something obvious.
1) When we are calling the open method of the http_request instance (http_request.open()) why are we specifing 'POST' as the HTTP method as the first parameter? Shouldn't it be 'GET' if we are to pass it a URL with a querystring? (as shown in the article)
2) Also, I am confused as to exactly how the server response is to send data back? Will the requested page on the server do processing depending on the data in the querystring it is passed, and then return this data? If so, how is the data passed back? Or does the whole content of a page (eg HTML) get sent back for the JS function to process?
An example of a called page sitting on the server in which the http_request object function is calling would have been useful.
Re 1) When you POST, you are again sending data from the browser to the server. This data is again formatted as a query string (e.g. "name=value&foo=bar"), only it's not visible in the browser. Well in the case of an XMLHTTPRequest the GETs are also not displayed in the browser, but that was just to clarify.
In any event if you want to post, you'll do:
http_request.open('POST', 'http://example.org/somepage.php', true); http_request.send('name=value&foo=bar');
If you want to GET: http_request.open('GET', 'http://example.org/somepage.php?name=value&foo=bar', true); http_request.send(null);
Hope this helps, Stoyan
This code works great in Firefox and Mozilla but IE doesn't seem to want to recognize that thhe XML doc has any children. I've been searching in vain for a solution to get this to work in IE but to no avail. Does anyone know where I might be able to find something on this?
This would fail if multiple ajax requests are being processed at the same time, wouldn't it be better to do somthing like:
That way the request isn't stored in a global variable. I haven't tested this code btw, but it seems reasonable to me.
I have a way to get around the single http_request resource and multiple requests to it. It still uses it as a global variable, but as an array. In a nutshell, it goes through the array and finds an available resource. If there isn't one, it creates a new one.
I'm rewriting some of this from the functions I've been testing, so something might break, but the general idea's there.
Here's a sample of it in use: http://www.digitalbonsai.com/xhrmulti.php