这行得通。我只是拆分了构建块以获得更好的可读性。
查看解释和内联 cmets 以了解其工作原理以及为什么必须这样制作。
当然,这不能用于检索跨域内容,因为您要么必须通过自己的脚本代理调用,要么考虑像 flXHR (Cross-Domain Ajax with Flash) 这样的集成
call.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>asd</title>
<script src="jquery.js" type="text/javascript"></script>
<script src="xmlDoc.js" type="text/javascript"></script>
<script src="output.js" type="text/javascript"></script>
<script src="ready.js" type="text/javascript"></script>
</head>
<body>
<div>
<input type="button" id="getit" value="GetIt" />
</div>
</body>
</html>
jquery.js 是(未压缩的 jQuery 1.3.2)
test.html 一个有效的 XHTML 文档
xmlDoc.js
// helper function to create XMLDocument out of a string
jQuery.createXMLDocument = function( s ) {
var xmlDoc;
// is it a IE?
if ( window.ActiveXObject ) {
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = "false";
// prevent erros as IE tries to resolve the URL in the DOCTYPE
xmlDoc.resolveExternals = false;
xmlDoc.validateOnParse = false;
xmlDoc.loadXML(s);
} else {
// non IE. give me DOMParser
// theoretically this else branch should never be called
// but just in case.
xmlDoc = ( new DOMParser() ).parseFromString( s, "text/xml" );
}
return xmlDoc;
};
output.js
// Output the title of the loaded page
// And get the script-tags and output either the
// src attribute or code
function headerData(data) {
// give me the head element
var x = jQuery("head", data).eq(0);
// output title
alert(jQuery("title", x).eq(0).text());
// for all scripttags which include a file out put src
jQuery("script[src]", x).each(function(index) {
alert((index+1)+" "+jQuery.attr(this, 'src'));
});
// for all scripttags which are inline javascript output code
jQuery("script:not([src])", x).each(function(index) {
alert(this.text);
});
}
ready.js
$(document).ready(function() {
$('#getit').click(function() {
$.ajax({
type : "GET",
url : 'test.html',
dataType : "xml",
// overwrite content-type returned by server to ensure
// the response getst treated as xml
beforeSend: function(xhr) {
// IE doesn't support this so check before using
if (xhr.overrideMimeType) {
xhr.overrideMimeType('text/xml');
}
},
success: function(data) {
headerData(data);
},
error : function(xhr, textStatus, errorThrown) {
// if loading the response as xml failed try it manually
// in theory this should only happen for IE
// maybe some
if (textStatus == 'parsererror') {
var xmlDoc = jQuery.createXMLDocument(xhr.responseText);
headerData(xmlDoc);
} else {
alert("Failed: " + textStatus + " " + errorThrown);
}
}
});
});
});
在 Opera 中,没有 createXMLDocument 和 beforeSend 函数,整个事情都可以工作。
Firefox (3.0.11) 和 IE6(无法测试 IE7、IE8、其他浏览器)需要额外的技巧,因为当服务器返回的 Content-Type: 未指示它是 xml 时,它们会出现问题.我的网络服务器为test.html. 返回了Content-Type: text/html; charset=UTF-8 在这两个浏览器中,jQuery 调用了error 回调,textStatus 说parsererror。因为在 jQuery.js 的第 3706 行
data = xml ? xhr.responseXML : xhr.responseText;
data 被设置为空。在 FF 和 IE 中,xhr.responseXML 为空。发生这种情况是因为他们不知道返回的数据是 xml(就像 Opera 一样)。只有xhr.responseText 设置了整个xhtml 代码。由于数据为空,第 3708 行
if ( xml && data.documentElement.tagName == "parsererror" )
抛出一个在第 3584 行捕获的异常,状态设置为 parsererror。
在FF中,我可以在发送请求之前使用overrideMimeType()函数解决问题。
但 IE 不支持 XMLHttpRequest 对象上的该功能,因此如果运行错误回调并且错误为 parsererror,我必须自己生成 XMLDocument。
test.html 示例
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Plugins | jQuery Plugins</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">var imagePath = '/content/img/so/';</script>
</head>
<body>
</body>
</html>