简体   繁体   English

ajax setTimeout在json_encode上不起作用

[英]ajax setTimeout not working on json_encode

AJAX function AJAX功能

var stopTime = 0;
 var checkRequApprove =  function ()
    {
      $.ajax({
      url: 'http://127.0.0.1/ProgVsProg/main/checkApproveReq',
      success:function(output){
        jsn=JSON.parse(output);
        if(output==false){
         stopTime = setTimeout(checkRequApprove, 3000); 
            }
        else {
              bootbox.dialog({
              message: "Battle",
              title: "Request Accepted",
              buttons:
               {
                  success: {
                  label: "Approve",
                  className: "btn-success",
                  callback: function() {
                       $.ajax({    
                        "type" : "POST",
                        "url" : "finalBattleApprove",
                        "data" : {'username' : jsn.user_name},
                       success: function(data){
                           $('#example').html(data);
                           $('#loadingmessage').show();
                        }
                     });
                  }
                  },
            }
    }
  });
}
  stopTime = setTimeout(checkRequApprove,3000);

Controller 调节器

public function checkApproveReq(){
    $id = $this->session->userdata('userID');
    $requApprove = '1';
    $check = $this->lawmodel->checkRequApprove($id,$requApprove);
    foreach($check as $row){
        if($row->requApprove == '1')
        {
        $reqID = $this->lawmodel->getID($row->requestedID);
        foreach($reqID as $row){
        echo json_encode(
        array(
             'user_name' =>$row->username,
            )
        );
    }
    }
    else
    echo false;
    }
}

I have this code wherein there is a realtime check to the database and if the condition has meet..custom dialog will pop up. 我有这段代码,其中有对数据库的实时检查,是否满足条件。.custom对话框将弹出。 Im having this problem on my checkRequApprove function..The bootbox.dialog will not showup if the condition is meet in the controller... i believe that my problem is because in the controller which echo json_encode . 我在checkRequApprove函数上遇到此问题。.如果在控制器中满足条件,则bootbox.dialog将不会显示...我相信我的问题是因为在控制器中echo json_encode I cant find any solution .im still a newbie in ajax.. 我找不到任何解决方案。我还是ajax的新手。

EDITED the custom dialog will only show after i refresh the page. 编辑后 ,自定义对话框仅在刷新页面后显示。

output is a string - the comparison output == false will always yield false, because a non-empty string is always true (not considering the edgecase "0" in which case "0" == false yields true). output是一个字符串-比较output == false将始终产生false,因为非空字符串始终为true(不考虑边缘情况“ 0”,在这种情况下,“ 0” == false产生true)。

Instead of 代替

if(output==false){
   ...
}

it should be: 它应该是:

if (!jsn){ // or jsn == false if you like that better
   ...
}

Also you should consider not returning a simple value in your controller, but always a proper json-object like: 另外,您应该考虑不要在控制器中返回简单的值,而应始终返回正确的json对象,例如:

else{    // consider always using brackets, for more robustness
    echo
         array(
             'error' => true
            );
}

Now, in your js you just check for 现在,在您的js中,您只需检查

if (jsn.error){
   ...
}

In any case, you should include an error callback for your json to handle possible errors with the json request. 无论如何,您应该为json包含一个错误回调,以处理json请求中可能出现的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM