VII.4. Debugging XMLHttpRequest CodeIt's not uncommon for early experimentation with the XMLHttpRequest object to be somewhat frustrating because its results are not rendered in the browser until it has successfully done its job. Here are some tips to follow when looking for the source of problems. These tips assume you either have access to a JavaScript debugger (such as the Venkman debugger for Mozilla) or use alerts or other ways of examining values of expressions while script statements are executing. VII.4.1. Inspect the readyState Property After a RequestAfter the send( ) method executes, you should be receiving frequent calls to the function handler bound to the readystatechange event. If the values never change from 1 or 2, you are probaby having either network or server problems. Verify that the URL you are supplying works in other environments, such as a browser. Also inspect the status and/or statusText properties of the request object to see if the server is issuing any error messages. Your goal is to obtain a final status value of 200. VII.4.2. Inspect the nodeType and childNodes of the responseXML PropertyIf the server sent the data to your request object, the responseXML.nodeType property should evaluate to 9, the nodeType value of a document node. After that, make sure that the XML data has been received such that the contents of the document are in the correct format. Test for the responseXML.childNodes.length property to verify that there is at least one child of the document node. The exact number will depend on the structure of your XML data, but a value of zero means no XML data is in the response. If either one of these tests fails, the most likely culprit is that the server sent the data in a content type that is not text/xml or one of the other valid XML content-types. First check the responseText property to see if the raw data is received in string form. If the data is there, but it's not showing up as an XML document node, you have content-type issues on the server. Setting the request header as shown in Examples VII-1 and VII-2 may help. Otherwise you will have to check the server configuration to make sure it outputs data for the URL with the correct content-type. VII.4.3. Make Sure Your XML is Well-FormedIt is also imperative that the XML output from your server be well-formed XML. Flaws in well-formedness commonly cause XMLHttpRequest to fail to convert the output to an XML document node. If you don't have an XML validator among your local tools, search the web for "xml validator" to find free third-party validators that should help you find problems in your output. |