如何更改.done下的响应(功能(数据)?

时间:2017-06-15 03:41:26

标签: javascript php html ajax if-statement

我正致力于创建面部检测系统。所以基本上使用网络摄像头拍照并检测该图片中是否有脸。

当有脸时我得到的回应是:

{"images":[{"status":"Complete","width":600,"height":450,"file":"content_5942003c94c10","faces":[{"topLeftX":259,"topLeftY":233,"height":188,"rightEyeCenterY":277,"rightEyeCenterX":314,"pitch":-8,"quality":0.20461,"confidence":0.99944,"chinTipX":350,"yaw":1,"chinTipY":430,"eyeDistance":88,"width":188,"leftEyeCenterY":280,"leftEyeCenterX":401,"attributes":{"lips":"Together","asian":0.99925,"gender":{"femaleConfidence":0.0031,"type":"M","maleConfidence":0.9969},"age":27,"hispanic":4.0e-5,"other":0.0007,"black":0,"white":1.0e-5,"glasses":"Eye"},"face_id":1,"roll":3}]}]}

没有脸时我得到的回答是:

{"Errors":[{"Message":"no faces found in the image","ErrCode":5002}]}

我想将响应更改为:如果有脸,如果没有脸,则提醒“成功”,提醒“失败”。我该怎么办?添加if / else语句?

Ajax:

$.ajax({
        type: 'POST',
        url: 'detect.php',
        data: data,
        dataType: 'json'
    }).done(function(data){

         console.log(data);
        $("#showCounter").html("");
        $("#detectResponse").html(data);
    });
    $(video).hide();
}

1 个答案:

答案 0 :(得分:1)

实际上你是对的,你需要在.done()中添加if语句

...        
}).done(function(data){
     if (data.images.length > 0){
         alert("success");
         //Do your codes
     }else{
         alert("fail");
         //Do your codes
     }
});

注意:在你的情况下,即使没有检测到面部,它仍然会落入.done(),因为它表明ajax成功返回数据。

相关问题