これは機能します。読みやすくするために、ビルディングブロックを分割しました。
説明とインラインコメントをチェックして、この動作と、なぜこのように作成する必要があるのかを把握してください。
もちろん、これを使用してクロスドメインコンテンツを取得することはできません。そのため、スクリプトを介して呼び出しをプロキシするか、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
and関数なしでbeforeSend
機能します。
Firefox(3.0.11)およびIE6(IE7、IE8、その他のブラウザーはテストできません)にはContent-Type:
、サーバーから返されたものがxmlであることを示さない場合に問題が発生するため、追加のトリックが必要です。私のウェブサーバーはContent-Type: text/html; charset=UTF-8
、test.html.
これら2つのブラウザで、jQueryがerror
コールバックを呼び出しましtextStatus
たparsererror
。jQuery.jsの3706行目
data = xml ? xhr.responseXML : xhr.responseText;
data
nullに設定されています。FFおよびIEと同様に、xhr.responseXML
はnullです。これは、返されたデータがxmlであることを彼らが取得しないために発生します(Operaのように)。そしてxhr.responseText
、xhtmlコード全体でのみ設定されます。データがnullであるため、3708行
if ( xml && data.documentElement.tagName == "parsererror" )
3584行でキャッチされ、ステータスがに設定されている例外をスローしますparsererror
。
FFではoverrideMimeType()
、リクエストを送信する前に関数を使用して問題を解決できます。
ただし、IEはXMLHttpRequestオブジェクトでその関数をサポートしていないため、エラーコールバックが実行され、エラーがである場合は、XMLDocumentを自分で生成する必要がありますparsererror
。
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>