|
Please don't use XML for such a simple request. Actually, I'm not convinced that XML is the best method for sending any data to JavaScript. I do a lot of work with XMLHTTPRequest, and I simply use the standard JavaScript object literal and array literal notations.
Now, I know the above is a simple demonstration, but for a simple flag response you need to return only a 1 or 0. To keep with the example, though, you can return a JS object:
{ method: 'checkName', result: 1 };
Then, the function is simplified:
function processReqChange()
{
// only if req shows "complete"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
--> eval('response = '+req.responseText);
eval(response.method + '(' + response.result + ');');
} else {
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
}
}
Obviously, for such a simple example there's little gain. But imagine trying to traverse an entire DOM tree for a large document. Using literals, you can create native JS objects and use the much simpler constructs such as "for/in" to manipulate the data. You can also directly address known data, as shown above.
Make the choice between this:
response.method
or this:
response.getElementsByTagName('method')[0].firstChild.data;
It also probably goes without saying, but the backend code is also greatly simplified by not having to create an XML document.
|