[英]Not getting the exact error message of laravel validation via AJAX
The message im getting in laravel is: 我在laravel中收到的消息是:
{message: "The given data was invalid.", errors: {oData:
["validation.required"]}}
errors: {oData: ["validation.required"]}
message: "The given data was invalid."
not the message itself but the validation.required message. 不是消息本身,而是validation.required消息。 when I append it to my HTML it display "validation.required" and not the error message that laravel provide.
当我将其附加到HTML时,它将显示“ validation.required”,而不是laravel提供的错误消息。
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: oParams['sUrl'],
type: oParams['sType'],
data: oParams['oData'],
contentType: oParams['mContentType'],
processData: oParams['mProcessData'],
success: function(aData)
{
console.log(aData);
},
error: function(request, status, error)
{
json = $.parseJSON(request.responseText);
$.each(json.errors, function(key, value){
$('.alert-danger').show();
$('.alert-danger').append('<p>'+value+'</p>');
});
$("#result").html('')
}
});
//Controller
public function addUser(Request $oRequest)
{
$aValidated = $oRequest->validate([
'oData' => 'required',
]);
return $aValidated;
}
The issue is you're running over the errors
object, which indeed, has a single key with an array containing the string validation.required
. 问题是您正在运行
errors
对象,该对象的确只有一个键,该键的数组包含字符串validation.required
。 What you should do instead is something like this: 相反,您应该做的是这样的:
json = $.parseJSON(request.responseText);
$.each(json, function(element){
$('.alert-danger').show();
$('.alert-danger').append('<p>'+ element.message +'</p>');
});
$("#result").html('')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.