2013-10-26 173 views
0

我需要使用ExtJS 3.4.0库分配响应值以在JavaScript函数中返回它,函数自然终止而不等待ajax响应,因此count的值始终为零,我需要知道我是否可以这样做,或者如果有替代方案。ExtJS 3.4.0等待Ajax响应

感谢

function test() { 
    var count = 0; 
    Ext.Ajax.request({ 
     url: "/Controller/Action", 
     method: "POST", 
     success: function(response) { 
      count = Ext.decode(response.responseText); 
     } 
    }); 

    if (count > 1) { 
     return false; 
    } 

    return true; 
} 

回答

0

您需要添加return语句在你成功的功能:

function test() { 
    var count = 0; 
    Ext.Ajax.request({ 
     url: "/Controller/Action", 
     method: "POST", 
     success: function(response) { 
      count = Ext.decode(response.responseText); 
      if (count > 1) { 
       this.other(false); 
      } 
      this.other(true); 
     }, 
     scope: this 
    }); 
}, 

function other(param){ 
    console.log(param); 
    } 
+0

嗨@Riku谢谢您的回答,一路上你所提到的收益只为成功函数但不是函数包装器。 有没有其他想法? – mmuniz

+0

尝试我更新的函数调用 – kuldarim