PDA

View Full Version : Store "Unable to parse JSON returned by Server"



DrunkenBeard
9 Sep 2011, 7:23 AM
Hi there,

I'm trying to make a list fetch its content from a database. Basically, I have a page named "JSONData.php" that when called with the appropriate parameters returns a valid JSON object. For the sake of troubleshooting I made that page always return the same JSON object : {"data":[{"lastname":"a", "firstname":"b"}, {"lastname":"c", "firstname":"d"}]}. Now to be completely sure that the problem wasn't with the returned JSON string I also made an Ajax request using Ext and verified that the page actually returned the string as expected.

The list code is pretty basic :



{ xtype: 'list',
store: listStore,
itemTpl: '<div class="contact"><strong>{firstname}</strong> {lastname}</div>'
}


Now here's the part that most likely isn't quite right :



var listStore = new Ext.data.Store({
autoLoad: true,
model: 'Contact',


proxy: {
type: 'ajax',
url : 'app/remote/JSONData.php',
actionMethods: {
read: 'POST'
}
},


reader: new Ext.data.JsonReader ({
root:'data',
})
});


Any idea why does this return a "Unable to parse JSON returned by Server" exception ?

Thanks :)

PS : I also make sure to register the "Contact" model using Ext.regModel.
PS 2 : The full exception actually reads : "Uncaught Ext.data.JsonReader.getResponseData: Unable to parse JSON returned by Server."

NickT
9 Sep 2011, 9:04 AM
In my uses of this, i have always defined the proxy to include the reader, like this. Not sure if that is what is causing your issue. I wouldn't think so, but it might be worth a try.


{ type: 'ajax',
url :'app/remote/JSONData.php',
reader: {
type: 'json',
root: 'data'
}
}

the other possible problem is the way you set actionMethods. I don't think you want to do it that way. Try setting just the read property after the store is instantiated.

listStore.proxy.actionMethods.read = 'POST';

tryanDLS
9 Sep 2011, 9:09 AM
Have you run your json thru jsonlint.com to verify that it's valid?