Extjs 获取Ajax请求中的url路径';s onSuccess函数
以下是我的Ajax请求代码:Extjs 获取Ajax请求中的url路径';s onSuccess函数,extjs,Extjs,以下是我的Ajax请求代码: for(some loop) { var SOME-VARIABLE = value; new Ajax.Request(SERVER-PATH + SOME-VARIABLE, { asynchronous: true, method: 'get', onSuccess: function(response) { //here I require SOME-VARIABLE } } 所以,我在循环中执行多
for(some loop) {
var SOME-VARIABLE = value;
new Ajax.Request(SERVER-PATH + SOME-VARIABLE, {
asynchronous: true,
method: 'get',
onSuccess: function(response) {
//here I require SOME-VARIABLE
}
}
所以,我在循环中执行多个ajax请求,请求工作得非常好。但我希望在相应的回调中请求url的变量(SOME-variable)部分的值。由于执行了多个请求,在回调中,我无法获得变量的实际值(它被后续循环迭代替换)。是否有任何方法可以访问Onsuccess
函数的响应对象中的请求url路径
谢谢。花括号在javasrcipt中没有作用域,但函数有作用域。因此,按函数将代码括在括号中:
for(some loop) {
var SOME-VARIABLE = value;
(function(url) {
new Ajax.Request(SERVER-PATH + url, {
asynchronous: true,
method: 'get',
onSuccess: function(response) {
//here You can access url
}
}
}) (SOME-VARIABLE);
您可以在请求中将某些变量存储为属性:
new Ajax.Request(SERVER-PATH + SOME-VARIABLE, {
asynchronous: true,
method: 'get',
options: {SOME-VARIABLE: SOME-VARIABLE},
onSuccess: function(response, obj) {
alert(obj.options.SOME-VARIALBE);
}
}
编辑:
使用Extjs 3.1和4.0.2实现了这一点:
var myOption = 'asdf';
Ext.get('myButton').on('click', function(){
Ext.Ajax.request({
url: 'http://jsfiddle.net/',
options: {myOp: myOption },
success: function(res, obj) {
alert(obj.options.myOp);
},
failure: function(res, obj) {
alert(obj.options.myOp);
}
});
});
看似简单的解决方案,但不起作用,我发现obj未定义。您使用的是哪个Ext版本?是的,这是版本问题,我使用的是Ext2.0.2,我无法升级它,因为此库是另一个(第三方)框架的一部分。谢谢你纠正我。