-2

我有一个读取 xml 文件的post()调用,因为这是一个正确的 xml 文件,所以我应该能够并且我应该使 dataType="xml"。但这打破了电话。如果我使 dataType="text" 效果很好。有没有人看到问题?

$.post('loadPage.php', { siteName : siteName},function(xml) {
Status2$.html(xml);
var bdystyle = $(xml).find("bodyStyle")[0].textContent;
var canvasstyle = $(xml).find("canvasStyle")[0].textContent;
},
"text"); 

php是:

<?php
    $siteName       = $_POST['siteName'];
    $fileName = "{$siteName}_sav.xml";
    $fileSize = filesize($fileName);
    $filePath =  $_SERVER['DOCUMENT_ROOT'] . "/" . $fileName;
    $site_fp = fopen( $filePath, 'r');
    $xml = fread($site_fp, $fileSize);
    echo $xml;
?>

xml文件是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
   <bodyStyle>background-image: none; background-color: rgb(255, 254, 253); cursor: auto;</bodyStyle>
   <canvasStyle>background-image: none; background-color: rgb(255, 26, 0);</canvasStyle>
   <canvasData>This is where the canvas html would go.</canvasData>
</root>
4

2 回答 2

0

我不知道为什么我不能将 dataType 指定为“xml”,但事实证明我不必为了让 jQuery 将下载的文件视为 XML 并为我提取各个组件,即这里的目标。如果使用 ajax 加载的文件是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<root>  
   <bodyStyle>body style will go here;</bodyStyle>
   <canvasStyle>canvas style will go here</canvasStyle>
   <canvasData>canvas data will go here</canvasData>
</root>

那么我需要提取文件的三个组成部分是:

$.post('loadPage.php', { siteName : siteName},function(xml) {
    var bodyStyle = $(xml).find("bodyStyle")[0].textContent;
    var canvasStyle = $(xml).find("canvasStyle")[0].textContent;
    var canvasData = $(xml).find("canvasData")[0].textContent;
});

loadPage.php 在哪里

<?php
    $siteName = $_POST['siteName'];
    $fileName = "{$siteName}.xml";
    $fileSize = filesize($fileName);
    $filePath =  $_SERVER['DOCUMENT_ROOT'] . "/" . $fileName;
    $site_fp = fopen( $filePath, 'r');
    $data = fread($site_fp, $fileSize);
    echo $data;
?>
于 2013-07-20T00:23:06.737 回答
0

我相信您的 php 代码需要发回正确的Content-Type标头,以便 jQuery(或某些浏览器?)正确解释它。

header('Content-Type: text/xml'); // or 'application/xml'
于 2013-07-19T04:31:31.157 回答