0

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ファイルです。

ありがとう

4

1 に答える 1

0

適切な XML から始める

<?xml version="1.0" encoding="ISO-8859-1"?> // or whatever
<section1>The section 1 text</section1>
<section2>The section 2 text</section2>
<section2>The section 3 text</section3>

filter()これらはルート要素であるため、 notを使用する必要がありfind()ますが、おそらく最適なオプションは、xml を別の要素に追加して、どちらの方法でも機能するようにすることです。

fileName = "abc.txt"

$.post('loadPage.php', {fileName : fileName}, function(xml) {
    var sect1 = $('<div />').append(xml).find("section1");
}, "xml");
于 2013-07-19T01:32:42.393 に答える