|
23
|
| Steve Hiner · 技术社区 · 17 年前 |
|
|
1
12
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> |
|
|
2
19
检查响应的内容类型。如果messages.xml的mime类型错误,Internet Explorer将不会将其解析为xml。
|
|
|
3
6
Quick&;肮脏的修复方法是将xml响应包装在html元素中,如div:
(适用于所有浏览器) |
|
|
4
5
您可能会发现,如果将数据类型传递给get调用,它可能会正确解析为XML。IE的怪癖可能会阻止jQuery将其自动检测为XML,从而导致将错误的数据类型传递给回调函数。
编辑:
|
|
|
5
3
|
|
|
6
3
你可以
并使用find()。它适用于IE8和firefox v.3.6.3 |
|
|
7
1
|
|
|
8
1
第二个问题是这样解决的:
|
|
|
9
1
|
|
|
10
1
无需更改find()。 |
|
|
11
1
所以,我分配
也就是
|
|
|
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
Chrome/Firefox:
IE8:
|
|
|
15
0
|
|
|
code-geek · Jquery根据单选按钮选择隐藏或显示文本字段 1 年前 |
|
|
Alex · 在轻量级中同时解构和不解构变量 1 年前 |
|
|
Ângelo Rigo · ReactJS映射:如何迭代[关闭] 1 年前 |
|
|
bairog · 从按属性筛选的对象数组字典中创建值数组 1 年前 |
|
|
lokiuucx · JS对象属性返回未定义,尽管对象属性应该有值 1 年前 |