0
votes

I'm trying to use Sencha Touch script tag proxy so that I can consume a JSON file on a remote website, however I am seeing parse errors in the Safari Console even though I have validated the JSON file is correct.

My Model is this:

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

My Store code is this:

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

Here is part of the JSON file on the remote server:

{
 "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."
    }
  ]
}

Finally the error I am seeing in Safari console shows below the first line

"title":json news", data.json:2SyntaxError: Parse error

Any help on this would be appreciated I have been scratching my head for a few hours on this one now.

Thanks Aaron

2

2 Answers

0
votes

Try changing it to this:

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

Also not that there is no 'id' in the json.

0
votes

After much trial and error, I found that this was not working due to my JSON file not being formatted correctly.

I followed this example: http://www.sencha.com/learn/legacy/Tutorial:Creating_JSON_Data_in_PHP

And with this code on my local server it now works fine.

<?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);
}

?>