Javascript 在设置变量之前,请等待Ext.Ajax.request成功响应
下面,您将看到一些为extjs 4应用程序设置当前登录用户的代码。如果我没有注释该警报,代码似乎会等到警报被接受后再继续(似乎是这样)。这样就有足够的时间让异步调用成功完成。如果我注释掉警报,变量“employeeFilter”永远不会被设置,因为AJAX调用没有及时返回。在这种情况下,它将“employeeFilter”设置为null。我如何修复这个问题,使它等待AJAX响应成功返回Javascript 在设置变量之前,请等待Ext.Ajax.request成功响应,javascript,ajax,json,extjs,asynchronous,Javascript,Ajax,Json,Extjs,Asynchronous,下面,您将看到一些为extjs 4应用程序设置当前登录用户的代码。如果我没有注释该警报,代码似乎会等到警报被接受后再继续(似乎是这样)。这样就有足够的时间让异步调用成功完成。如果我注释掉警报,变量“employeeFilter”永远不会被设置,因为AJAX调用没有及时返回。在这种情况下,它将“employeeFilter”设置为null。我如何修复这个问题,使它等待AJAX响应成功返回 var loggedInUserId = null; Ext.Ajax.request({ url:
var loggedInUserId = null;
Ext.Ajax.request({
url: '/Controls/UserList/UserService.asmx/GetLoggedInUserId',
method: 'POST',
jsonData: { 'x': 'x' },
success: function (response, opt) {
loggedInUserId = Ext.decode(response.responseText).d;
},
failure: function (response) {
}
});
//alert(loggedInUserId);
var employeeFilter = loggedInUserId;
var projectFilter = '-1';
我会这么做的
var employeeFilter;
Ext.Ajax.request({
url: '/Controls/UserList/UserService.asmx/GetLoggedInUserId',
method: 'POST',
jsonData: { 'x': 'x' },
success: function (response, opt) {
employeeFilter = Ext.decode(response.responseText).d;
//Do here whatever you need to do once the employeeFilter is set. probably call a function and pass the employee filter as parameter.
},
failure: function (response) {
}
});
var projectFilter = '-1';
听起来你应该在回调函数中分配
employeeFilter
值。AJAX中的“A”表示“异步”,我有大约50行代码(已经使用了不止一次),因此,我不希望在ajax成功调用中设置employeefilter后立即将50行代码包装成一个函数并调用该函数。