24
|
Steve Hiner · 技术社区 · 16 年前 |
![]() |
1
12
检查响应的内容类型。如果将messages.xml作为错误的mime类型,Internet Explorer将不会将其解析为xml。 要检查内容类型,需要访问xmlhttpRequest对象。正常成功回调不会将其作为参数传递,因此需要添加通用的AjaxComplete或AjaxSuccess事件处理程序。这些事件的第二个参数是xmlhttpRequest对象。可以对其调用getResponseHeader方法以获取内容类型。
不幸的是,我在Internet Explorer中没有办法覆盖服务器发送的内容,因此如果它是错误的,您需要更改服务器以发送内容类型的“text/xml”。
有些浏览器具有
|
![]() |
2
19
由于IE的问题在于它的XML解析器阻塞了未使用正确的“text/xml”头传递的XML文件,因此可以在 AJAX完成 事件: complete: function( xhr, status ) { alert( "COMPLETE. You got:\n\n" + xhr.responseText ) ; if( status == 'parsererror' ) { alert( "There was a PARSERERROR. Luckily, we know how to fix that.\n\n" + "The complete server response text was " + xhr.responseText ) ; xmlDoc = null; // Create the xml document from the responseText string. // This uses the w3schools method. // see also if( window.DOMParser ) { parser=new DOMParser(); xmlDoc=parser.parseFromString( xhr.responseText,"text/xml" ) ; } else // Internet Explorer { xmlDoc=new ActiveXObject( "Microsoft.XMLDOM" ) ; xmlDoc.async = "false" ; xmlDoc.loadXML( xhr.responseText ) ; } $( '#response' ).append( '<p>complete event/xmlDoc: ' + xmlDoc + '</p>' ) ; $( '#response' ).append( '<p>complete event/status: ' + status + '</p>' ) ; processXMLDoc( xmlDoc ) ; } }, 下面是一个更完整的例子<!DOCTYPE html> <html> <head> <title>Reading XML with jQuery</title> <style> #response { border: solid 1px black; padding: 5px; } </style> <script src="jquery-1.3.2.min.js"></script> <script> function processXMLDoc( xmlDoc ) { var heading = $(xmlDoc).find('heading').text() ; $( '#response' ).append( '<h1>' + heading + '</h1>' ) ; var bodyText = $(xmlDoc).find('body').text() ; $( '#response' ).append( '<p>' + bodyText + '</p>' ) ; } $(document).ready(function() { jQuery.ajax({ type: "GET", url: "a.xml", // ! watch out for same // origin type problems dataType: "xml", // 'xml' passes it through the browser's xml parser success: function( xmlDoc, status ) { // The SUCCESS EVENT means that the xml document // came down from the server AND got parsed successfully // using the browser's own xml parsing caps. processXMLDoc( xmlDoc ); // IE gets very upset when // the mime-type of the document that // gets passed down isn't text/xml. // If you are missing the text/xml header // apparently the xml parse fails, // and in IE you don't get to execute this function AT ALL. }, complete: function( xhr, status ) { alert( "COMPLETE. You got:\n\n" + xhr.responseText ) ; if( status == 'parsererror' ) { alert( "There was a PARSERERROR. Luckily, we know how to fix that.\n\n" + "The complete server response text was " + xhr.responseText ) ; xmlDoc = null; // Create the xml document from the responseText string. // This uses the w3schools method. // see also if( window.DOMParser ) { parser=new DOMParser(); xmlDoc=parser.parseFromString( xhr.responseText,"text/xml" ) ; } else // Internet Explorer { xmlDoc=new ActiveXObject( "Microsoft.XMLDOM" ) ; xmlDoc.async = "false" ; xmlDoc.loadXML( xhr.responseText ) ; } $( '#response' ).append( '<p>complete event/xmlDoc: ' + xmlDoc + '</p>' ) ; $( '#response' ).append( '<p>complete event/status: ' + status + '</p>' ) ; processXMLDoc( xmlDoc ) ; } }, error: function( xhr, status, error ) { alert( 'ERROR: ' + status ) ; alert( xhr.responseText ) ; } }); }); </script> </head> <body> <div> <h1><a href="http://think2loud.com/reading-xml-with-jquery/">Reading XML with jQuery</a></h1> <p> <a href="http://docs.jquery.com/Ajax/jQuery.ajax#options">#1 jQuery.ajax ref</a> </p> </div> <p>Server says:</p> <pre id="response"> </pre> </body> </html> .xml的内容<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 它延伸 this example . |
![]() |
3
6
数据类型:“xml”并不能在IE8中解决这个问题,而是通过“typeerror”预期来解决。 快速修复是将XML响应包装在HTML元素中,如DIV:
(适用于所有浏览器) |
![]() |
4
5
您可能会发现,如果将数据类型传递到get调用中,它可能会正确地解析为XML。IE的怪癖可能会停止jquery自动检测它为XML,从而导致传递给回调函数的数据类型错误。
编辑: 实际上,我刚刚体验到.find()在任何浏览器中都不适用于项目,但我可以使用.filter()。我不得不求助于这个,这很烦人,但如果它奏效的话……
|
![]() |
5
3
我也有同样的问题,但是我用下面的代码修复了ie jquery xml.find()问题。 注意:使用.text()而不是.html()。
|
![]() |
6
3
你可以做到
并使用find()。它适用于IE8和Firefox v.3.6.3。 |
![]() |
7
1
有时IE将换行符作为额外的节点读取。尝试删除标签上多余的空白,或者尝试将其封装为CDATA。 |
![]() |
8
1
当我从XML文档中检索数据时,遇到了同样的问题。在网上搜索了很多之后,我找到了这个网站,但是没有正确的答案。但有一个答案帮助我解决了这个问题: 由于IE的问题是它的XML解析器阻塞了未使用正确的“text/xml”头传递的XML文件,因此可以在Ajax完成事件中包含一些代码: 在进行$.Ajax(…)和$.get(…)调用时,我发现了IE的两个问题:
第二个问题是这样解决的:
|
![]() |
9
1
尝试告诉jquery它得到的数据类型,以便它使用正确的方法来处理您的请求。 |
![]() |
10
1
更改以下内容。
到
无需更改find()。 |
![]() |
11
1
导入电子邮件联系人时,我也遇到了同样的问题。我可以导入联系人并在除IE外的所有浏览器中显示
所以,我指派
即
早些时候
|
![]() |
12
0
我也遇到了同样的问题,我正在开发一个基于Web的应用程序,但是我需要它在一张CD中离线部署。我在这一页找到了解决方案,和你在上面看到的解决方案一样。 http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests 代码非常简单:
|
![]() |
13
0
我也有同样的问题… 解决方法如下: http://www.w3schools.com/dom/dom_parser.asp
使用它将var转换为xml对象… |
![]() |
14
0
它工作得很好!!!!试试这个, 铬/火狐:
IE8+/Safari:
IE8:
这里是示例代码,
|
![]() |
15
0
如果XML是由PHP脚本生成的,那么可以
那么find方法在每个浏览器上都有效 |
![]() |
code-geek · Jquery根据单选按钮选择隐藏或显示文本字段 5 月前 |
![]() |
Alex · 在轻量级中同时解构和不解构变量 5 月前 |
![]() |
Ângelo Rigo · ReactJS映射:如何迭代[关闭] 5 月前 |
![]() |
bairog · 从按属性筛选的对象数组字典中创建值数组 5 月前 |
![]() |
lokiuucx · JS对象属性返回未定义,尽管对象属性应该有值 6 月前 |