I have such results and I need to append them in my view:
val: {email: Array(1), first_name: Array(1)}
email: Array(1)
0: "The email field is required."
length: 1
__proto__: Array(0)
first_name: Array(1)
0: "The first name field is required."
length: 1
__proto__: Array(0)
__proto__: Object
code
$('#submitRegister').on('click', function(e) {
e.preventDefault();
$.ajaxSetup({
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
});
var formData = {};
$('#registerForm').find("input[name]").each(function (index, node) {
formData[node.name] = node.value;
});
$.ajax({
url: '{{route('frontend.register.validate')}}',
type: "POST",
data: {
"_token": "{{ csrf_token() }}",
formData,
},
dataType: "json",
success:function(data) {
$(data.errors).each(function (index, value) {
console.log('val: ', value);
});
}
});
});
html
<div id="errors"></div>
any idea?
2
Answers
You can use
document.getElementById()
to find the element you want to append things to, then you can use document.createElement() to create an element to append, and then you can useappend()
to append it to the div. So, all together:You should be able to put this in your .each() function, after the console.log().
You need to use Object.keys then check and loop inside each array