PDA

View Full Version : Some variables from the server answer?



Alien
30 Jan 2010, 5:51 PM
Kind time of days! How it is possible to receive some variables from the server answer?
Tried here so:


Ext.Ajax.request({
url: 'test.php',
params: { var1: 'test1', var2: 'test2' },
success : function(response, o) {
Ext.Msg.alert('Success','Var1='+o.result.var1+', Var2='+o.result.var2);
},
failure : function() {
Ext.Msg.alert('Error','Not Found');
}
});

test.php:

if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
{
exit('{success:true, var1:'.json_encode($_REQUEST['var1']).', var2:'.json_encode($_REQUEST['var2']).'}');
}

But does not work if to use response.responseText that outputs all html page.
Help to understand please, in advance thanks!

tobiu
31 Jan 2010, 12:12 PM
success: function (result, request) {
var jsonData = Ext.util.JSON.decode(result.responseText);
console.log(jsonData.var1);
}

kind regards,
tobiu

Alien
31 Jan 2010, 5:25 PM
Does not work

var jsonData = Ext.util.JSON.decode(result.responseText);
console.log(jsonData.var1);
Ext.Msg.alert(var1);
and

var jsonData = Ext.util.JSON.decode(result.responseText);
var1 = console.log(jsonData.var1);
Ext.Msg.alert(var1);

tobiu
1 Feb 2010, 3:22 PM
come on, you
should know what firebug is.



success: function (result, request) {
var jsonData = Ext.util.JSON.decode(result.responseText);
alert(jsonData.var1);
}


if not...

Alien
2 Feb 2010, 4:17 AM
please help

umarkashmiri
2 Feb 2010, 4:25 AM
Ext.Ajax.request({
url: 'test.php',
params: { var1: 'test1', var2: 'test2' },
success : function(response) {
Ext.Msg.alert(response.responseText);
},
failure : function() {
Ext.Msg.alert('Error','Not Found');
}
});

just alert it and see what it says...let me know if you still feel problems.

Thanks,
UmarKashmiri

tobiu
2 Feb 2010, 5:12 AM
i wrote an answer but it was not submitted. strange.



success: function (result, request) {
var jsonData = Ext.util.JSON.decode(result.responseText);
alert(jsonData.var1);
}


learn about firebug.

kind regards,
tobiu

fay
2 Feb 2010, 5:13 AM
What was posted (JSON.decode) does work, so I'm guessing that your JSON is invalid. Just to be sure:

Use FireFox+FireBug; put a breakpoint in your success handler; copy the value of "result.responseText" to www.jsonlint.com and confirm that your JSON is valid.

Alien
2 Feb 2010, 5:50 AM
thanks

Mike Robinson
2 Feb 2010, 8:01 AM
The best thing to do is: use FireBug (Network tab... XHS...) to look at what the client is sending to the host and what the host is sending back to the client.

"Do not 'assume' ... anything."

In the ExtJS code, the response will first come back as a (JSON encoded) string, which (as others have said... and as you well know...) you must decode back into a JavaScript structure. (Ext.decode.) Add some breakpoints in your code at that point to see if you actually got the string that you expected, and if it actually was decoded into what you thought it should be.

The hardest part of debugging a problem like this is just ... seeing it. Once you can see it plainly, the solution is usually obvious.