The Road to xmlHTTPRequest
http://developer.apple.com/internet/webcontent/xmlhttpreq.html
First I went here. I copied, pasted, everything worked.
I modified slightly. I found out that you could NOT make it so that
if (req.readyState 4) {
// only if "OK"
if (req.status 200) {
reads as
if (req.readyState 4 && req.status 200)
because the callback function gets called more than once. Since the readyState WILL NOT be 4 on the first call, the error message will occur, even if the expected result DOES happen afterward.
(Thank you, #EFNet.)
Also, I’ve learned a little something about callback functions. When you set an event handler name to a function, you’re setting a reference to that function name. YOU CANNOT PASS ARGUMENTS to a callback function in that assignment statement.
(Thank you, MSDN.)
As far as I can tell, you have to have a global variable or something that the function can access if you need for the function to have arguments.
XHConn is supposed to make using xmlHTTPRequest a little easier. I think it’s easy enough without the library, though.
Things I’ve learned to check:
1) Make sure the backend script is sending a Content-Type of text/xml
PHP: header(‘Content-Type: text/xml’);
2) When using a non-unicode program with Windows, and you have Windows set to use…oh…say…Japanese…as the default language for non-unicode programs (I don’t even know what unicode is), then you have to make sure that the program isn’t outputting funky 2-byte gibberish into the front of the files you’re working with, especially .php files where you’re trying to use sessions. The session data is the first thing that has to be output to the browser, and that 2-byte gibberish screws it up.
3) Don’t try to access .innerHTML for page elements that haven’t loaded yet. Use something like
<body onload="myfunction();>
to ensure that the page elements have loaded before trying to access them.