jQuery .post() 呼び出しで読みたいテキスト ファイル "abc.txt" があります。このファイルには、section1、section2、および section3 の 3 つのセクションがあります。セクション 1、セクション 2、およびセクション 3 のテキストを個別に引き出すことができるように、jQuery で巧妙なことをしたいと考えています。私が最初に考えたのは、ファイルを変更して、次のように xml タイプの括弧でセクションをマークすることでした。
<section1>The section 1 text</section1>
<section2>The section 2 text</section2>
<section3>The section 3 text</section3>
次に、post() 呼び出しは次のようになります。
fileName = "abc.txt"
$.post('loadPage.php', {fileName : fileName},function(xml) {
var sect1= $(xml).find("section1");
},
"xml"); // dataType
しかし、これはいくつかのレベルで失敗します。まず、最後の「xml」データ型が post() の動作を妨げているようです。私の小さな xml のようなタグは、それが xml データであると ajax に思い込ませなかったと思います。第 2 に、dataType を省略した場合は返されますが、$(xml).find("section1"); 爆発します。
ここで機能するものに近づいていますか?
loadPage.php は次のようになります。
<?php
$siteName = $_POST['siteName'];
$fileName = "{$siteName}_sav.html";
$fileSize = filesize($fileName);
$filePath = $_SERVER['DOCUMENT_ROOT'] . "/" . $fileName;
echo ("<br /> reading $fileSize bytes from $filePath");
$site_fp = fopen( $filePath, 'r');
$xml = fread($site_fp, $fileSize);
if($xml) {
echo ("<br />xml: " . htmlspecialchars($xml));
}
else {
echo ("<br />Read from $fileName failed");
}
?>
それが違いを生む場合、実際にはtxtファイルではなくhtmlファイルです。
ありがとう