【问题标题】:condition statement in ajax success functionajax成功函数中的条件语句
【发布时间】:2012-01-30 11:41:01
【问题描述】:

我正在使用 Codeigniter MVC 框架并且我正在努力使用 Jquery Ajax。 我的问题是如何在 ajax 成功函数中放置条件语句?

基本上我想要做的是在成功输入后它会重定向到另一个页面,如果没有会显示错误:手机号码不匹配或为空请填写空白。

控制器方法

if($mobile_number == "") {
                    $data = array('message' => "Please fill up the blanks", 'success' => 'no');

            }
            elseif($mobile_number != "123") {
                    $data = array('message' => "Please does not match", 'success' => 'no');

            }else {
                    #redirect('home/test');
                    $data = array('message' => "Mobile match.", 'success' => 'yes');
            }
            $output = json_encode($data);
            echo $output;

my_ajax.js

$(document).ready(function(){


$("#request_submit").click( 

function(){

    var mobtel=$("#mobtel").val();

    $.ajax({
    type: "POST",
    url: "post_action",
    dataType: "json",
    data: "mob_tel="+mobtel, 
    cache:false,
    success: function (data) {

            if(data.success == 'yes') {
                    $("#form_message").html(data.message).fadeIn('slow'); 
            }
            else if(data.success == 'no') {
                    alert('error');
            }
    }

    });
});
});

提前致谢。希望有人可以在这里帮助我。 (T_T)

【问题讨论】:

  • 我认为你不应该修剪data,因为它已经是一个解析对象而不是字符串。
  • 此外,您的成功函数中的第二个 if 语句应该是 else if 语句,因为 data.success 不能同时是 'yes''no'
  • 更新了我的代码。还是不行。您可以查看我的网站:apps.stratpoint.com:9114/home/request_verification
  • 尝试在 echo $output 后​​添加退出;在你的 php 代码中
  • 使用我的jsfiddle example function在控制台登录数据对象的结构,查看服务器实际返回的是什么。

标签: jquery ajax codeigniter


【解决方案1】:

不确定我是否很好地理解了您的问题...从我得到的信息来看,这应该会推动正确的方向。

如果我是你,我会将我的回复更改为以下格式:

$data = array(
 'message'=> /* Your message */,
 'success' => /* True or false */, 
 'case'=> /* 1,2,3 something unique to tell the cases apart */,);

echo json_encode($data);

所以,在 jQuery 中我可以简单地做到这一点:

 $.ajax({
   type: "POST",
   url: "post_action",
   dataType: "json",
   data: "mob_tel="+mobtel, 
   cache:false,
   success: function (data) {

        switch(data.case){
        1: /*First case */
        break;
        2: /*Second... */
        break;
        3: /* You know the drill... */
        break;
        default:
        /* If none of the above */

        }
}

});

由于您的代码中有三种情况,最好将它们作为三种情况处理。更可靠的代码,更不容易出错。

我希望这会有所帮助!

【讨论】:

    【解决方案2】:

    在控制器方法中:

    函数结束时使用exit();

    在 my_ajax.js 中

    $(document).ready(function(){
    
    
    $("#request_submit").click( 
    
    function(){
    
        var mobtel=$("#mobtel").val();
    
        $.ajax({
        type: "POST",
        url: "post_action",
        dataType: "json",
        data: "mob_tel="+mobtel, 
        cache:false,
        success: function (data) {
    
                if(data.success == 'yes') {
                        $("#form_message").html(data.message).fadeIn('slow'); 
                }
                else{
    
                      if(data.message == "Please does not match"){ 
                        //show error message where ever you want
                          $("#form_message").html(data.message).fadeIn('slow'); 
                      }else{
                        //Do whatever you want to do :: to redirect can use window.location
                           alert('data.message')
                       }
    
                }
        }
    
        });
      });
    });
    

    【讨论】:

    • 我不确定 CI,但在某些情况下,您需要在回显对 ajax 调用的响应之前清理输出缓冲区。这可以通过使用 ob_clean();
    • 如果在 javascript 或 php 中显示任何错误?请让我知道错误..使用错误控制台..
    【解决方案3】:

    您可以使用 for 循环来搜索回调对象中的任何键

    如果您想查看回调中是否存在错误键,您可以使用:

    success: function(cb)
    {
        for(errors in cb)
        {
           //checking for error key in the callback
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 2011-11-10
      • 1970-01-01
      相关资源
      最近更新 更多