PDA

View Full Version : Connection.Request with json encode Problem



ks2009
26 Apr 2009, 7:37 PM
Hello,

I have a code that sent the values to server using Ext.data.Connection, here the script as follows:


var conn = new Ext.data.Connection();
var reqid = conn.request({
url: 'modules/usergroups.php',
success: function(resp, opt)
{
try
{
var s = remove_NL(resp.responseText);
var result = Ext.util.JSON.decode(s);//*****
if(result.success==false)
{
msg.hide();
Ext.Msg.alert('Result',result.errorMsg);
}
else
{
msg.hide();
var groupid = result.groupid;
var records =storeServices.getModifiedRecords();
if(records.length > 0 )
{
var fArray=[];
Ext.foreach(storeServices.getModifiedRecords(),
function(r)
{

fArray.push(r.data); }
var s_json = Ext.util.JSON.encode(fArray);
//send data again to server using Ext.data.connection
);

}

}
}

catch(err)
{
Ext.Msg.alert('ERROR','Could not decode : ' + resp.responseText);
}

},
failure: function(resp,opt)
{
msg.hide();
Ext.Msg.alert('ERROR',resp.statusText);
},
params: {

name: name,
description: description,
active: active,
admin: admin,
//other field removed for readability
mode: 'INSERT_GROUP' }
});

When this code is executed, the server will return the following:


{success;true, groupid: [num]}
My problem is when I removed above text with red color json.decode works fine, but when I put those codes in red, code with ******, json.decode failed , it cannot decode the string : {success;true, groupid: [num]}

I don't know what is wrong with this code.

Thanks.

jay@moduscreate.com
27 Apr 2009, 3:48 AM
what does remove_NL do? are you *sure* it's valid json afterwards?

ks2009
27 Apr 2009, 4:56 AM
Yes, it is still a valid JSON after calling that function, that function removes \n\r from the string. My problem is when I removed those codes in red font, the script works perfectly.

jay@moduscreate.com
27 Apr 2009, 5:57 AM
place a break point just after : var s_json = Ext.util.JSON.encode(fArray);


see what s_json resolved to

ks2009
27 Apr 2009, 6:21 AM
it cannot reached there when I place those codes in red font, there is an exception decoding the JSON string see my code with **** , it directly executes the code under catch block.

Condor
27 Apr 2009, 6:24 AM
What is the EXACT server response? Try running it through jslint.com to check if it is valid JSON.

ps. {success;true,groupid:[num]} isn't valid JSON!

ks2009
27 Apr 2009, 6:35 AM
{success:true, groupid: 20}, where 20 will change depending on the number returned it is a sequential number from a database. This JSON string is successfully decoded when I removed my code in red font.

Condor
27 Apr 2009, 6:41 AM
Ext.foreach(storeServices.getModifiedRecords(), ...) ?

Shouldn't that be: Ext.each(records, ...)

ks2009
27 Apr 2009, 6:56 AM
Yes, I tried that, and it worked now, I am just wondering why I am not getting any runtime error, well anyway, thank you for all the help.