我使用 PHP 提交表单,然后使用 $.ajax()。这是我的 $.ajax 方法,成功后可以完美运行。当我想显示错误时,很难循环每个responseText。请参阅以下 $.ajax() 代码:
$.ajax({
// ... ,
error: function(errors){
$.each(errors, function(index, error){
info.hide().find('ul').append('<li>'+error+'</li>');
});
info.slideDown();
}
});
我检测到所有问题,但我对它如何仅显示错误感到困惑。请在 http://prntscr.com/7nt3lq 上查看我在屏幕上的输出。 我只想显示以下错误字段: 如果我将其设置为: "name":["The name field is required."],"fname":["The fname field is required."]
error: function(errors){
console.log(errors);
}
然后屏幕输出为http://prntscr.com/7nt9tv。通过循环,如何消除异常错误 422(无法处理的实体)并仅检索响应文本?
问题是
error
回调的第一个参数不是 respose
数据,而是 jqXHR
对象。
$.ajax({
url: '/echo/asdf',
// ... ,
error: function (jqxhr) {
if (jqxhr.status = 422) {
var errors = JSON.parse(jqxhr.responseText);
$.each(errors, function (index, error) {
info.hide().find('ul').append('<li>' + error + '</li>');
});
info.slideDown();
}
}
});