【问题标题】:Ext Js ajax request not working for failure functionExt Js ajax请求不适用于失败功能
【发布时间】:2012-06-13 09:19:49
【问题描述】:

这是我的函数发送 ajax 请求以检查电子邮件重复,同时从我的 php 文件中获取响应,而不是失败函数,它仅在成功函数中返回,为此我已检查 result.responseText == 为“false”并使用else 条件现在在这种情况下,它也会显示错误弹出消息,但不要中止请求继续执行并保存数据

    function email_check(userType) {
      //alert(userType);
   var extmail=   Ext.Ajax.request({
                  url: '<?= Extjs_renderer::ajaxurl();; ?>ajax/emailcheck',

                  success: function ( result, request ) {
                   if(result.responseText=='false'){
                       //Ext.Ajax.abort(extmail); tried
                       Ext.MessageBox.alert('Error', "email already exist");
                      // return false;

                    //Ext.getCmp('email').setValue(''); works

                   }else {
                       return true;
                   }


                    },
                    failure: function(response, options) {

                    Ext.MessageBox.alert('Error', "email already exist fail");

                    },
                  params: {merc_mem_tab:userType }
                  });

  }

这是我的 ajax.php 代码

      function  emailcheck(){
    $get_email=$this->db->query("select * from customers where email='".$_REQUEST['merc_mem_tab']."'");
    if($get_email->num_rows==0){
        echo "true";
                    return true;
    }else{
        echo "false";
       // echo "{success: true}";
        return false;

    }

}

在我的面板处理程序上,我也在尝试检查响应但未能成功

                         if('<?= $this->controller->name; ?>'=="customers"){
                        //alert(Ext.getCmp('email'))

                         if(email_check(Ext.getCmp('email').getValue()) == false){

                             return false;
                         }
                     }

【问题讨论】:

    标签: extjs extjs3 extjs-mvc


    【解决方案1】:

    你不能从 ajax 请求返回,它是异步的,这段代码if(email_check(Ext.getCmp('email').getValue()) == false) 不会等待答案。 正如 Imad 所说,失败也只是针对 http 失败而不是针对错误响应。你检查响应错误的代码是正确的,但我建议你在成功函数上调用一个保存方法。像:

     function email_check(userType) {
          //alert(userType);
       var extmail=   Ext.Ajax.request({
                      url: '<?= Extjs_renderer::ajaxurl();; ?>ajax/emailcheck',
                      scope: this,
                      success: function ( result, request ) {
                       if(result.responseText=='false'){
                           Ext.MessageBox.alert('Error', "email already exist");
                           //do nothing else
                       }else {
                           this.saveData();    
                       }
                        },
                        failure: function(response, options) {
                        Ext.MessageBox.alert('Error', "Communication failed");
                        },
                      params: {merc_mem_tab:userType }
                      });
    
      }
    

    【讨论】:

    • 嗯,我认为这是一个更好的建议,我会在明天之前尝试这个,谢谢你的帮助
    【解决方案2】:

    成功或失败回调的选择基于 HTTP 响应代码。所以,如果你想达到失败功能,你必须做一些事情:

    function  emailcheck(){
        $get_email=$this->db->query("select * from customers where email='".$_REQUEST['merc_mem_tab']."'");
        if($get_email->num_rows==0){
            echo "true";
            return true;
        }else{
            throw new Exception("Error : Email Already Exists !");
        }
    }
    

    它应该会引发错误 500(未处理的异常),ExtJS 会将其识别为失败响应。

    【讨论】:

    • 它在故障功能上工作,但不停止执行它继续保存数据我想停止它
    • 奇怪...你说抛出异常后的php代码被执行了?
    猜你喜欢
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2018-11-30
    • 2018-04-22
    • 2012-12-21
    相关资源
    最近更新 更多