2014-01-08 132 views
0

我有一个关于我的代码在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> 

控制器

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"; 
     } 
} 

我的问题是AJAX会时刻警惕,即使它不符合我的控制器的条件。 我不能测试我的项目,如果它是正确的,或者使用笨not..Im ..

进出口新的阿贾克斯PLSS帮助.. :( 编辑

我怎样才能使警报弹出只有当它满足条件在我的控制器?:( 就像当Cscore ==“1”的警报会弹出..

+0

什么是你想怎么办?从你的问题 – Steve

+0

编辑已经不明显..sorry..im不是很明确.. :( – Zurreal

回答

0

如果我明白你的问题的权利,那么这行

if(output !=' '){ 

在你的要求应该是类似于

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

为什么?
当您不符合$Cscore == '1'$Rscore == '1'时,返回字符串"Answer the question"。并且知道无论你返回的将是变量的内容,它将永远不会只是一个空间' '

+0

我甚至尝试..但它没有工作.. – Zurreal

0

将foreach代码移到模型中,而是使用return而不是echo。

说: 型号:

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