PDA

View Full Version : Uncaught Ext.Error decode an invalid JSON String



gigofizz
30 Dec 2011, 9:05 AM
Anyone can guide the solution, please

Record a value and spaces are saved whith _

Uncaught Ext.Error: Unable to parse the JSON returned by the server: You're trying to decode an invalid JSON String: array(1) { ["{"cat_codigo":"8","cat_nombre":"Categoria_8","cat_descripcion":"Categoria_8","cat_estado":"1"}"]=> string(0) ""}

Return result in the grid whith _

30383

mitchellsimoens
30 Dec 2011, 9:28 AM
Plug your JSON string into jsonlint.com

gigofizz
30 Dec 2011, 9:40 AM
Results: Valid JSON

[
{
"cat_codigo": "8",
"cat_nombre": "Categoria_8",
"cat_descripcion": "Categoria_8",
"cat_estado": "1"
}
]

The problem is when:
Record a value for example: 'CATEGORY 8' with space.
Record a value en phpmyadmin: CATEGORY_8.

mitchellsimoens
30 Dec 2011, 9:49 AM
Ext.create('Ext.grid.Panel', {
renderTo : Ext.getBody(),
width : 400,
height : 400,
store : Ext.create('Ext.data.Store', {
fields : ['cat_codigo', 'cat_nombre', 'cat_descripcion', 'cat_estado'],
autoLoad : true,
proxy : {
type : 'ajax',
url : 'data.json',
reader : 'json'
}
}),
columns : [
{
header : 'Description',
flex : 1,
dataIndex : 'cat_descripcion'
}
]
});


[
{
"cat_codigo": "8",
"cat_nombre": "Categoria 8",
"cat_descripcion": "Categoria 8",
"cat_estado": "1"
}
]

That worked for me with 4.1.0 beta 1 and 4.0.2a

gigofizz
30 Dec 2011, 9:55 AM
In my Model have:


Ext.define('SG.crm.model.Categorie', { extend: 'Ext.data.Model',

idProperty: 'cat_codigo',


fields: [
{ name: 'cat_codigo',

},
{ name: 'cat_nombre',
type: 'string'
},
{ name: 'cat_descripcion',
type: 'string'
},
{ name: 'cat_estado'
}
],
proxy: {
type: 'rest',
url : '/rest/crm/categories/',
batchActions: false,
reader: {
type: 'json',
root: 'categories',
totalProperty: 'totalCount'
}
}
});

skirtle
30 Dec 2011, 3:48 PM
Are you really sure that your JSON is what you think it is? Judging by that error I'd say that your server is returning this:


array(1) { ["{"cat_codigo":"8","cat_nombre":"Categoria_8","cat_descripcion":"Categoria_8","cat_estado":"1"}"]=> string(0) ""}

While there's some valid JSON in the middle there's a load of other stuff before and after it that will cause parsing to fail. It definitely isn't the same as the JSON you posted as 'valid' because at the very least there are double quotes between the [ and the {.

Inspect the response in the Network tab to make sure.

thilakmsc
13 Jul 2012, 9:25 PM
Error Message :

'Unable to parse the JSON returned by the server: You're trying to decode an invalid JSON String: <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">
{"Table" : [{"UserId" : "1","UserName" : "Thilakraj","DateOfBirth" : "3/21/1985 12:00:00 AM","Gender" : "1","EmailId" : "thilakraj@angleritech.com"},
{"UserId" : "2","UserName" : "thilak","DateOfBirth" : "7/10/2012 12:00:00 AM","Gender" : "1","EmailId" : "thilakraj@angleritech.com"},
{"UserId" : "3","UserName" : "thilaksample","DateOfBirth" : "7/10/2012 12:00:00 AM","Gender" : "1","EmailId" : "thilakraj@angleritech.com"},

</string>'
when calling method: [nsIDOMEventListener::handleEvent]
[Break On This Error]

Filtered chrome url chrome://firebug/content/net/spy.js





Json Data :

{"Table" : [{"UserId" : "1","UserName" : "Thilakraj","DateOfBirth" : "3/21/1985 12:00:00 AM","Gender" : "1","EmailId" : "thilakraj@angleritech.com"},
{"UserId" : "2","UserName" : "thilak","DateOfBirth" : "7/10/2012 12:00:00 AM","Gender" : "1","EmailId" : "thilakraj@angleritech.com"}]}


Code :

Ext.onReady(function () {

Ext.define('Bond', {
extend: 'Ext.data.Model',
fields: ['UserId', 'UserName', 'DateOfBirth', 'Gender','EmailId']
});


var store = new Ext.data.JsonStore
({
root: 'Table', // see json output
proxy: {
type: 'ajax',
url: '../ExtJs.asmx/UserInformation',
actionMethods: {create: 'POST', read: 'POST', update: 'POST', destroy: 'POST'},
reader:
{
type: 'json',
root: 'Table',
idProperty: 'UserId'
}
},
model: 'Bond'
// fields: [ {name: 'UserId', type: 'int'}, 'UserName','DateOfBirth','Gender','EmailId']
});

store.load();


var grid = new Ext.grid.GridPanel({

store: store,
columns: [
{header: "User Name", width: 120, dataIndex: 'UserName', sortable: true},
{header: "Date Of Birth", width: 180, dataIndex: 'DateOfBirth', sortable: true},
{header: "Email", width: 115, dataIndex: 'EmailId', sortable: true}
,{header: "Gender", width: 100, dataIndex: 'Gender', sortable: true}
],
stripeRows: true,
autoExpandColumn: 'UserName',
height: 350,
width: 600,
title: 'User Information',
stateful: true,
stateId: 'grid'
});

store.load();

grid.render('grid-example');
});

skirtle
14 Jul 2012, 5:55 AM
@thilakmsc.

Please use [CODE] tags when posting code.

From what you've posted it looks like you have invalid JSON. There's a stray comma near the end and, more importantly, it seems that you're wrapping the JSON in XML tags.

What do you see as the response coming back from the server in the Net tab?