PDA

View Full Version : Parameter for Ext.Ajax.request



ragecf
30 Jul 2013, 7:54 AM
Hi, all

I have two questions about Ext.Ajax.request

1. how to pass the respones.text out of success?




function myFun(param) {
Ext.Ajax.request({

url: 'xxx.php'
params: {action: 'getXX'},
success: function(response) {

var returnMsg = Ext.decode(response.text); \\returnMsg value is normal here.


}


});

var a = returnMsg;
console.log(a); \\ var a is empty, how to get returnMsg value here?

}



2. How to pass parameters to success of Ext.Ajax.request?



function myFun(param) {
Ext.Ajax.request({

url: 'xxx.php'
params: {action: 'getXX'},
success: function(response) {


console.log(param); \\param value is undefined , how to get param value here?


}


});


}

tobiu
30 Jul 2013, 8:12 AM
logging a variable that is not defined anywhere won't work.

the success method has 2 parameters:


success: function(response, opts)


try console.log(opts).

best regards
tobiu

dawesi
30 Jul 2013, 8:23 AM
As success is called outside the scope of the ajax call, it has no references to it.

Some notes:
1) you are creating an annonymous instance of Ext.Ajax also by not creating a variable.
2) also using var in:


var returnMsg

makes returnMsg private to the success function.

-------------

you could do a couple of things:

1) listen for the 'requestcomplete' event in your controller (or in listeners) on the ajax call and react (good practice)

or

2) fire a custom event, passing the response into the event (then listen to it with your app in your controller)

or

directly pass the value into an existing variable or function in your app. (not good practice)


success: function(){

success: function(response) {

var returnMsg = Ext.decode(response.text);
MyApp.utils.DoSomething.dealWiththis(returnMsg);


}


}

ragecf
31 Jul 2013, 6:18 PM
As success is called outside the scope of the ajax call, it has no references to it.

Some notes:
1) you are creating an annonymous instance of Ext.Ajax also by not creating a variable.
2) also using var in:


var returnMsg

makes returnMsg private to the success function.

-------------

you could do a couple of things:

1) listen for the 'requestcomplete' event in your controller (or in listeners) on the ajax call and react (good practice)

or

2) fire a custom event, passing the response into the event (then listen to it with your app in your controller)

or

directly pass the value into an existing variable or function in your app. (not good practice)


success: function(){
success: function(response) {

var returnMsg = Ext.decode(response.text);
MyApp.utils.DoSomething.dealWiththis(returnMsg);


}


}

Thank you for provide so great solution.

I solved my problem with solution 2 although it is not good practice.

For solution 1, I don't know how to listen to requestcomplete event for a single Ajax request within a function. I can add a global listener for ajax requestcomplete event, but I think global listener is not good.