@mabdullahsari I changed that section to:
xhr.onreadystatechange = function () {
const message = JSON.parse(xhr.response);
if (this.readyState === XMLHttpRequest.DONE) {
if (this.status == 200) {
var div = document.getElementById('msg');
for (var key in message) {
div.innerHTML += message[key];
}
}
if (this.status == 422) {
var div = document.getElementById('msg');
div.innerHTML += xhr.response;
for (var key in xhr.responseText) {
div.innerHTML += message[key];
}
}
}
}
The 200 works and displays all okay. However the 422 still does nothing.
Note however that in the background laravel still prevents that field from being blank.
And still in network tab it's showing 200 on failed validation.
And fetch js showed 405. I also tried
const message = JSON.parse(xhr.responseText);
Edit: Like I said, jquery gets the 422. And the docs state:
When using the validate method during an AJAX request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.
So surely it should work using plain ajax, without special coding to send a 422. This is very hard to troubleshoot.
But I am not giving up.