我正在製作面部識別系統。我使用了稱爲Kairos的API。我得到的響應是面部特徵數據或來自非面部圖像的錯誤消息。我該如何改變回應並將它們顯示在屏幕上,例如「成功!這是一張臉」或「沒有臉」。我嘗試過if/else語句,但似乎沒有任何迴應。我應該怎麼做?如何基於ajax響應提醒你想要的任何響應
<script>
$("#testDetect").click(function() {
var file = $('#imageFile')[0].files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function() {
var imageData = parseImageData(reader.result);
var data = {};
data.image = imageData;
$.ajax({
\t url : "http://localhost/Karios/simple-detect/form-post.php",
\t type : "POST",
\t data : data,
\t dataType : 'text'
\t }).done(function(response) {
console.log(response);
if (!response) { // Something unexpected happened. The message body is empty.
alert('Hmm, unexpected response from Kairos');
} else if (response['Errors'] && response['Errors'].size() > 0) { // If Errors is defined in the response, something went wrong.
if (response['Errors'][0]['ErrCode'] == 5002) { // This appears to be the error when no faces are found.
alert(response['Errors'][0]['Message']);
} else {
alert('Some other error occurred:\n' + response['Errors']['ErrorCode'] + ': ' + response['Errors']['Message']);
}
} else { // If there are no errors in the response, can we assume it detected a face? I guess so.
alert('Face(s) detected');
// The response has a ton of information about what it saw, including gender, age, ethnicity
// and more.
}
})
}
});
到底是什麼問題?你的代碼是否工作? 'alert'和'console.log'是否按預期工作?爲什麼不把響應放入頁面上的元素? –
除了'.done()'之外,你還嘗試過使用'.fail()'嗎? – larz
可以在響應成功時分享輸出,哪裏不是? – rafrsr