XMLHttpRequest 在本地工作,但不在服务器上工作
简而言之,我有一个在本地运行良好的项目,但是一旦上传到我的服务器,我的一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
服务器上的 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 theresponseXML
property is not being set. Note that theresponseText
property does contain the XML text.Fix the HTTP server to return the correct content-type.
据我所知,它使 AJAX 请求正常。根据我的计算,你的问题是这些行:
xmldoc is null,因此它抛出以下错误:
我也很好奇为什么当你已经加载了 jQuery 时你还要费力手动创建自己的 XMLHttpRequest 对象反正。为什么不直接使用 jQuery.ajax 呢?您可以将参数设置为包含
xml
的dataType
,即使您没有正确设置 HTTP 标头,这也可能会强制解析它。It's making the AJAX request fine from what I see. By my reckoning, your problem are these lines:
xmldoc is null, thus it's throwing the following error:
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
ofxml
, which might coerce it in to parsing it even if you aren't setting your HTTP headers properly.您是否在您最喜欢的浏览器中查看 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 thetext/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 withoverrideMimeType()
.