我的目标是加载XML以存储在Ext JS中,然后在网格中显示数据。
我已经准备了可以生成XML数据的PHP
<?php
/** create-xml.php */
$books = [
[
"isbn" => "9780071799355",
"title" => "Oracle Database 12c SQL",
"author" => "Jason Price"
],
[
"isbn" => "0672328666",
"title" => "Sams Teach Yourself Oracle PL/SQL in 10 Minutes",
"author" => "Ben Forta"
]
];
$dom = new DomDocument('1.0', 'UTF-8');
$root = $dom->createElement("books");
$dom->appendChild($root);
foreach ($books as $b) {
$book = $dom->createElement("book");
$isbn = $dom->createElement("isbn", $b["isbn"]);
$book->appendChild($isbn);
$title = $dom->createElement("title", $b["title"]);
$book->appendChild($title);
$author = $dom->createElement("author", $b["author"]);
$book->appendChild($author);
$root->appendChild($book);
}
//$dom->formatOutput = true;
echo $dom->saveXML();
很显然,我检查了文件create-xml.php在本地主机上生成XML
<?xml version="1.0" encoding="UTF-8"?>
<books><book><isbn>9780071799355</isbn><title>Oracle Database 12c SQL</title><author>Jason Price</author></book><book><isbn>0672328666</isbn><title>Sams Teach Yourself Oracle PL/SQL in 10 Minutes</title><author>Ben Forta</author></book></books>
我尝试将其加载到依赖于ext-all-dev.js的JS脚本中。
var xmlbookStore = Ext.create("Ext.data.Store", {
fields: ["isbn", "author", "title"],
autoLoad: true,
proxy: {
type: "ajax",
url: "create-xml.php",
//url: "books.xml",
reader: {
type: 'xml',
record: 'book',
rootProperty: 'books',
idPath: 'isbn'
}
}
});
console.log(xmlbookStore);
Ext.onReady(function () {
var grid = Ext.create("Ext.grid.Panel", {
store: xmlbookStore,
title: "Books",
columns: [
{
text: "ISBN",
dataIndex: "isbn"
},
{
text: "Author",
dataIndex: "author"
},
{
text: "Title",
dataIndex: "title"
}
],
renderTo: Ext.getBody()
});
});
我觉得很奇怪,当我将数据源切换到普通的XML文件'books.xml'(从相同的数据生成并保存到磁盘)时,完全相同的代码可以工作。
答案 0 :(得分:1)
解决方案:
使用XML Reader时,服务器响应必须以XML格式发送回数据。我不知道这是否是最好的方法,但是您可以尝试使用此方法(create-xml.php
文件中的另一行):
<?php
...
header('Content-type: text/xml');
echo $dom->saveXML();
?>