简体   繁体   English

总是使用ajax弹出警报

[英]alert always pop up using ajax

I have a problem about my code in ajax. 我对ajax中的代码有疑问。

AJAX AJAX

<script type="text/javascript">
 var stopTime =0;
 var scoreCheck =  function ()
    {
      $.ajax({
      url: 'http://127.0.0.1/ProgVsProg/main/checkScoreRoundOne',
      success:function(output){
        if(output !=' '){
          $('#maincontent1').html(output);
            bootbox.alert("We have a winner", function(){     
            });    
         stopTime = setTimeout(scoreCheck, 1000); 
            }
        else {
          clearTimeout(stopTime);
            }
    }
  });
}
stopTime = setTimeout(scoreCheck,1000);
 </script>

CONTROLLER 控制器

public function checkScoreRoundOne(){
$id = $this->session->userdata('userID');
$battleID = $this->lawmodel->getBattle($id);
foreach($battleID as $row){
    $Rscore = $row->requestedScore;
    $Cscore = $row->challengerScore;
    if($Cscore == '1'){
        $rID = $this->lawmodel->getID($row->challengerID);
        foreach($rID as $row){
          echo $row->username."Got the correct answer";
           }
        }
    else if($Rscore == '1'){
        $cID =$this->lawmodel->getID($row->requestedID);
        foreach($cID as $row){
            echo $row->username."Got the correct answer";
          }
        }
    else 
        echo "Answer the question";
        }
}

My problem is ajax will always alert even it did not meet the condition in my controller. 我的问题是,即使不符合我的控制器中的条件,ajax也会始终发出警报。 I cant test my project if it is correct or not..Im using codeigniter.. 我无法测试我的项目是否正确..我正在使用codeigniter ..

Im new in ajax plss help..:( Edited 我是ajax plss帮助中的新手。.:( 编辑

How can i make the alert popup only when it meet the condition in my controller? 仅当警报弹出框符合控制器中的条件时,我才能如何使其弹出? :( Like when Cscore == '1' alert will popup.. :(就像当Cscore =='1'警报会弹出..

If i get your question right, then this line 如果我正确回答您的问题,那么此行

if(output !=' '){

in your request should be something like 在您的请求应该是这样的

if(output != "Answer the question"){

Why? 为什么?
When you don't meet $Cscore == '1' or $Rscore == '1' you return the string "Answer the question" . 当您不符合$Cscore == '1'$Rscore == '1'将返回字符串"Answer the question" And knowing that whatever you return will be the content of the output variable it will never be only a space ' ' ! 而且知道返回的内容将是output变量的内容,所以它永远不会只是空格' '

Move the foreach code to the model and instead use return instead of echo. 将foreach代码移至模型,然后使用return代替echo。

Say: Model: 说:型号:

public function getID($row->challengerID){
 //some code
if($Cscore || $Rscore){
    foreach($cID as $row){
        return $row->username."Got the correct answer";
      }
  }else{
  return false;
  }

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

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