PDA

View Full Version : You're trying to decode an invalid JSON String



softball
13 Jun 2014, 1:13 AM
Dear ?I have the sample as follow? but on chrome console ,it show:
Ext.JSON.decode(): You're trying to decode an invalid JSON String: <br />
<b>Notice</b>: Undefined index: param in <b>C:\xampp\htdocs\ext4\04\4.16_p.php</b> on line <b>5</b><br />
[{cname:"L1_1",id:"110000" },{cname:"L1_2",id:"150000"} ] <br />

why?



<script type="text/jscript">
Ext.require('Ext.*');
Ext.onReady(function(){
Ext.define('State', {
extend: 'Ext.data.Model',
fields: [
{type: 'string', name: 'cname'},
{type: 'int', name: 'id'}

]
});


var store = Ext.create('Ext.data.Store', {
model: 'State',
proxy: {
type: 'ajax',
url: '4.16_p.php?act=sheng&n='+new Date().getTime()+''
},
autoLoad: true,
remoteSort:true
});


var store1 = Ext.create('Ext.data.Store', {
model: 'State',
proxy: {
type: 'ajax',
url: '4.16_p.php?act=shi&n='+new Date().getTime()+''
},
autoLoad: false,
remoteSort:true
});


var store2 = Ext.create('Ext.data.Store', {
model: 'State',
proxy: {
type: 'ajax',
url: '4.16_p.php?act=qu&n='+new Date().getTime()+''
},
autoLoad: false,
remoteSort:true
});



Ext.create("Ext.panel.Panel",{
renderTo: document.body,
width:290,
height:220,
title:"3 Level",
plain: true,
margin:'30 10 0 80',
bodyStyle: "padding: 45px 15px 15px 15px;",
defaults :{
autoScroll: true,
bodyPadding: 10 },
items:[{
xtype:"combo",
name:'sheng',
id : 'sheng',
fieldLabel:'Chose L1',
displayField:'cname',
valueField:'id',
store:store,
triggerAction:'all',
queryMode: 'local',
selectOnFocus:true,
forceSelection: true,
allowBlank:false,
editable: true,
emptyText:'Chose L1',
blankText : 'Chose L1',
listeners:{
select:function(combo, record,index){
try{
//userAdd = record.data.name;
var parent=Ext.getCmp('shi');
var parent1 = Ext.getCmp("qu");
parent.clearValue();
parent1.clearValue();
parent.store.load({params:{param:this.value}});
}
catch(ex){
Ext.MessageBox.alert("ERR","data load err?");
}
}
}
},
{
xtype:"combo",
name:'shi',
id : 'shi',
fieldLabel:'Chose L2',
displayField:'cname',
valueField:'id',
store:store1,
triggerAction:'all',
queryMode: 'local',
selectOnFocus:true,
forceSelection: true,
allowBlank:false,
editable: true,
emptyText:'Chose L2',
blankText : 'Chose L2',
listeners:{
select:function(combo, record,index){
try{
//userAdd = record.data.name;
var parent = Ext.getCmp("qu");
parent.clearValue();
parent.store.load({params:{param:this.value}});
}
catch(ex){
Ext.MessageBox.alert("ERR","data load err?");
}
}
}
},
{
xtype:"combo",
name:'qu',
id : 'qu',
fieldLabel:'Chose L3',
displayField:'cname',
valueField:'id',
store:store2,
triggerAction:'all',
queryMode: 'local',
selectOnFocus:true,
forceSelection: true,
allowBlank:false,
editable: true,
emptyText:'Chose L3',
blankText : 'Chose L3',
}
]
});


});
</script>



------------------------4.16_p.php


<?php


$act=$_GET["act"];
$param=$_REQUEST["param"];


if ($act="sheng"){
echo " [{cname:\"L1_1\",id:\"110000\" },{cname:\"L1_2\",id:\"150000\"} ] ";

}



if ($act="shi"){
if ($param="110000") {
echo "[{cname:\"L2_1\",id=\"110100\"},{cname:\"L2_2\",id=\"110200\"}]";
}else if ($param="150000"){
echo "[{cname:\"L2_3\",id=\"150100\"},{cname:\"L2_4\",id=\"150200\"}]";
}

}


if ($act="qu"){
if ($param="110100") {
echo "[{cname:\"L3_1\",id=\"110101\"},{cname:\"L3_2\",id=\"110102\"}]";
}else if ($param="110200"){
echo "[{cname:\"L3_3\",id=\"110201\"},{cname:\"L3_4\",id=\"110202\"}]";
}else if ($param="150100"){
echo "[{cname:\"L3_5\",id=\"150101\"},{cname:\"L3_6\",id=\"150202\"}]";
}else if ($param="150200"){
echo "[{cname:\"L3_6\",id=\"150101\"},{cname:\"L3_8\",id=\"150202\"}]";
}

}
?>

f.baron
13 Jun 2014, 2:11 AM
Try escaping the attribute names, for example:

echo " [{\"cname\":\"L1_1\",\"id\":\"110000\" },{\"cname\":\"L1_2\",\"id\":\"150000\"}] ";

softball
15 Jun 2014, 4:32 PM
Dear f.baron? Thx, But it still show err message? [E] Ext.JSON.decode(): You're trying to decode an invalid JSON String: ........

and the combo doesn't show option ,and no work when I click it!

vallesquino
15 Jun 2014, 11:27 PM
Hi,

I think that PHP are not retrieving a valid String, so Ext.JSON.decode is not working. Try to use the following to see if it works :)

echo " '[{\"cname\":\"L1_1\",\"id\":\"110000\" },{\"cname\":\"L1_2\",\"id\":\"150000\"}]' ";

Regards

softball
16 Jun 2014, 5:27 PM
Dear?
It still show [E] Ext.JSON.decode(): You're trying to decode an invalid JSON String:

Maybe , It will always show thoese message.

I had try another sample ? it show thoes message in chrome's console,but it can show data on store.


$r="";

$r.="{";
$r.="total:";
$r.="$counts,";
$r.="items:[";
for ($i=$start+1;$i<=$Ls;$i++){
$r.="{";
$r.="id:$i,";
$r.="title:\"newTitle$i\",";
$r.="author:\"newAuthor$i\",";
$r.="hits:$i,";
$r.="addtime:\"".date("Y/m/d")."\"}";

if ($i<$Ls){
$r.=(",");
}
}
$r.="]";
$r.=("}");
echo $r;




SO?


listeners:{
select:function(combo, record,index){
try{
//userAdd = record.data.name;
var parent = Ext.getCmp("qu");
parent.clearValue();
parent.store.load({params:{param:this.value}});
}
catch(ex){
Ext.MessageBox.alert("ERR","UNLOAD DATA?");
}
}


Does it Ok to passs params by parent.store.load({params:{param:this.value}}); ?
Because I use
$param=$_REQUEST["param"]; on tagregt php.
but its show
Undefined index: param in ....

thx.

cmeans
20 Jun 2014, 7:56 AM
Ignore (for the moment) the "You're trying to decode an invalid JSON String", because that's just indicating you're getting a server-side error response instead of pure JSON.

The bit you should focus on is the actual (underlying) server-side error: " Undefined index: param"

I don't know PHP, but my guess is that your code here:


$param=$_REQUEST["param"];

is what's triggering the error.

From what I can tell, the parameters you're sending are "act" and "n" (I assume "n" is just to make sure you're not getting a cached response).

So your PHP code should probably be modified to:


$act=$_REQUEST["act"];

...then you can process the value of $act.

-Chris

softball
22 Jun 2014, 4:23 PM
Dear Chris?
Thx?
I had used the $_GET or $_REQUEST for $act? It's Ok.
But the
$act=$_REQUEST["param"];

it may be from " parent.store.load({params:{param:this.value}}); " in the ext,

In this sample? It may to send the param when I chose the option .