XMLHttpRequest 在本地工作,但不在服务器上工作

发布于 2024-11-29 19:10:47 字数 303 浏览 0 评论 0原文

简而言之,我有一个在本地运行良好的项目,但是一旦上传到我的服务器,我的一个 XMLHttpRequest 就会严重失败。正在加载的 XML(本例中为 .tmx)文件的相对路径绝对是正确的。任何解决此问题的帮助将不胜感激。

该项目的位置是 www.jorum.se/fancypants/,相关代码位于 game.js(第 22 行)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春有你 2024-12-06 19:10:47

服务器上的 XML 文档未使用 text/xml 内容类型提供,因此 XmlHttpRequest 对象不会将响应视为 XML,这意味着 responseXML属性未设置。请注意,responseText 属性确实包含 XML 文本。

修复 HTTP 服务器以返回正确的内容类型。

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.

盗琴音 2024-12-06 19:10:47

据我所知,它使 AJAX 请求正常。根据我的计算,你的问题是这些行:

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

xmldoc is null,因此它抛出以下错误:

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

我也很好奇为什么当你已经加载了 jQuery 时你还要费力手动创建自己的 XMLHttpRequest 对象反正。为什么不直接使用 jQuery.ajax 呢?您可以将参数设置为包含 xmldataType,即使您没有正确设置 HTTP 标头,这也可能会强制解析它。

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.

像极了他 2024-12-06 19:10:47

您是否在您最喜欢的浏览器中查看 JavaScript 错误?我在 game.js 第 22 行看到一个错误,看起来是因为您的 responseXML 不是您所期望的,因此 xmldoc 没有按照您希望的方式进行初始化。打开调试器(Chrome 检查器或 Firefox 中的 Firebug)并亲自查看失败的原因。如果是我,我会在 game.js 的第 21 行设置一个断点,然后查看 req 对象,看看它告诉我有关上一个事务的信息(错误、其他数据等...)。

请参阅 Mozilla 参考文献中的 responseXML 说明responseXML 为空的可能原因是服务器未应用 text/xml Content-Type 标头或 XML 解析错误。如果您的服务器未设置正确的 MIME 类型,您可以使用 overrideMimeType() 强制将其解析为 XML。

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文