我正在尝试触发 Bootstrap Growl如果 $.post
调用失败,则显示消息。我怎么知道它是否失败?因为后端部分(PHP 脚本)返回此 JSON:
{
"success":false,
"errors":{
"usuario":{
"captcha":[
"The captcha is not valid."
]
}
}
}
所以我在 jQuery 中做了这个:
$.post($form.attr('action'), $form.serialize(), function(result) {
// ... Process the result ...
}, 'json').fail(function(result) {
console.log("fail");
$.growl({
icon: "fa fa-paw",
title: "<strong>Error en datos</strong>",
message: "Los siguientes errores ocurrieron",
type: "danger",
allow_dismiss: true,
animate: {
enter: 'animated bounceInDown',
exit: 'animated bounceOutUp'
}
});
});
但是消息或 console.log()
都不起作用,所以我不知道正确的是 .fail()
还是 .error( )
或者总是成功,然后我需要验证 JSON 的 success
是否带有 FALSE
这意味着一些错误。
作为第二个小问题,我如何在 JSON 中移动寻找错误字符串,以便在 Growl 元素的 message
中将它们显示为 LI?
最佳答案
如果响应包含http错误,例如400或500,则将触发fail子句。如果http响应是200 - 好的,那么您将不会失败。
关于javascript - "error"或 "fail"用于 jQuery 中的 $.post Ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25518706/