ajax JSON 成功总是触发,而错误=>true



我不明白,每次成功都会被触发,而我的控制器的返回是{error=>'true'}。这是我的JS代码:

$.ajax({
            url: root_url + 'getInfo/' + idClient,
            async: false,
            dataType: 'json',
            success: function(data){
                for (var x = 0; x < data.retour.length; x++) {
                    content += data.retour[x].startdate;
                    if(data.retour[x].aconfirmer == 1)
                        content += '&nbsp;&nbsp;<i class="fa fa-warning" title="À confirmer"></i>';
                    content += "<br>";
                }
            },
            error: function(e){
                console.log(e.responseText);
            }
        });

这是我的控制器:

function getInfo($idClient)
{
    header('Content-type: application/json');
    try
    {
        if($info= $this->infos->getInfos($idPatient))
        {
            echo json_encode(array('success'=>'true', 'retour' => $info));                
        }
        else
        {
            echo json_encode(array('error'=>'true'));
            //return false;
        }
    }
    catch(Exception $e)
    {
        echo json_encode(array('error'=>'true', 'Message'=>$e->getMessage()));
        return false;
    }
}

在我的检查员那里,我可以看到退货:{"error":"true"}

您已经在代码中捕获了错误,并返回了一个带有{error:true}的常规响应。问题是请求成功并返回200 OK,所以它触发了成功函数,才能调用错误函数,http请求必须完全失败,500404等…

要做你想做的事,你必须做以下之一:

  • 1) 未发现错误
  • 2) 通过错误状态代码
  • 3) 将此处理为成功函数,并检查错误标志是否为响应中为true

相关内容

最新更新