简体   繁体   中英

XMLHttpRequest working locally, but not on server

In short, I have a project which runs fine locally, but once uploaded to my server, my one XMLHttpRequest fails miserably. The relative path to the XML (.tmx in this case) file being loaded is definitely correct. Any help in resolving this matter would be greatly appreciated.

The location of the project is www.jorum.se/fancypants/ , and the code in question in game.js (line 22).

The XML document on the server is not being served with the text/xml content-type and so the XmlHttpRequest object is not treating the response as XML, which means that the responseXML property is not being set. Note that the responseText property does contain the XML text.

Fix the HTTP server to return the correct content-type.

It's making the AJAX request fine from what I see. By my reckoning, your problem are these lines:

var xmldoc = req.responseXML;
    var mapwidth = xmldoc.documentElement.getAttribute("width");

xmldoc is null, thus it's throwing the following error:

game.js:22
Uncaught TypeError: Cannot read property 'documentElement' of null

I'm also curious as to why you're going through the effort of manually making your own XMLHttpRequest object when you've already loaded jQuery anyway. Why not just use jQuery.ajax ? You could set your parameters to include a dataType of xml , which might coerce it in to parsing it even if you aren't setting your HTTP headers properly.

Are you looking at javascript errors in your favorite browser? I see an error on line 22 of game.js which looks like it's because your responseXML isn't what you were expecting it to be and thus xmldoc isn't getting initialized the way you want it to be. Crack open a debugger (Chrome inspector or Firebug in Firefox) and see for yourself what is failing. If it were me, I'd set a breakpoint on line 21 of game.js and look at the req object to see what it's telling me about the last transaction (errors, other data, etc...).

See the decription of responseXML here on Mozilla's reference . Possible reasons for a null responseXML are server doesn't apply the text/xml Content-Type header or an XML parsing error. If your server isn't setting the right MIME type, you can force it to parse as XML with overrideMimeType() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM