Sencha Touch脚本标记代理JSON Reader解析错误

时间:2011-10-05 07:08:01

标签: javascript json sencha-touch

我正在尝试使用Sencha Touch脚本标记代理,以便我可以在远程网站上使用JSON文件,但是我在Safari控制台中看到解析错误,即使我已经验证了JSON文件是正确的。

我的模特是这样的:

    Ext.regModel('NoteNewsModel', {
    idProperty: 'id',
    fields: [
        { name: 'id', type: 'int' },
        { name: 'title', type: 'string' },
        { name: 'description', type: 'string' }
        // { name: 'icon', type: 'string' }
    ]
});    

我的商店代码是:

Ext.regStore('NotesNewsStore', {
    model: 'NoteNewsModel',
    proxy: {
        type: 'scripttag',
        url: 'myjsonurl',
        reader: new Ext.data.JsonReader ({
            type: 'json',
            root: 'entries'
        })
    },
    autoLoad: true
 });

以下是远程服务器上JSON文件的一部分:

{
 "title":"json news",
 "link":"https://myurl.com/json-news.html",
 "description":"",
 "language":"en",
 "copyright":"my domain",
 "ttl":"120",
 "entries":[
    {
     "title":"SmarterMail Upgrade",
     "link":"https://mydomain.com/122.html",
     "date":"1316414335",
     "guid":"https://mydomain.com/122.html",
     "author":"flank plank",
     "description":"test entry",
     "introtext":"testing the intro text."
    }
  ]
}

最后,我在Safari控制台中看到的错误显示在第一行

下方

“title”:json news“, data.json:2SyntaxError:解析错误

对此的任何帮助都将不胜感激我现在已经在这个问题上抓了几个小时。

谢谢Aaron

2 个答案:

答案 0 :(得分:0)

尝试将其更改为:

reader: {
            type: 'json',
            root: 'entries'
        }

也不是说json中没有'id'。

答案 1 :(得分:0)

经过多次试验和错误,我发现由于我的JSON文件格式不正确而无法正常工作。

我按照这个例子:http://www.sencha.com/learn/legacy/Tutorial:Creating_JSON_Data_in_PHP

在我的本地服务器上使用此代码,它现在可以正常工作。

<?php 

$link = mysql_pconnect("server", "user", "password") or die("Could not connect");
mysql_select_db("database") or die("Could not select database");

$arr = array();
$rs = mysql_query("SELECT * FROM news");

while($obj = mysql_fetch_object($rs)) {
    $arr[] = $obj;
}

$callback = $_REQUEST['callback'];
if ($callback) {
    header('Content-Type: text/javascript');
    echo $callback . '(' . json_encode($arr) . ');';
} else {
    header('Content-Type: application/x-json');
    echo json_encode($arr);
}

?>