I am working on a laravel project where i am mkaing use of Jquery Ajax to validate the form which is returning a 422 status but i need a 200 status
@brand12 No, you really don’t 😬
200 means "OK". The response was not “OK” if there are validation errors.
How are on earth are you intending to distinguish between successful responses and error responses if it just returns 200 OK every time?
Laravel returns a 422 status for validation errors because it’s a good convention to do so. So handle requests properly instead of trying to override good conventions with absolutely terrible ones.
Execute your request. Change how you handle the response based on the status code:
$('#loginform').on('submit', function (event) {
event.preventDefault();
$.ajax({
data: new FormData(event.target),
dataType: 'json',
error: function (xhr) {
// Login request errored, so actually handle errors
// i.e. display validation messages next to inputs
$.each(xhr.response.errors, function (field, messages) {
// Display messages next to field
});
},
method: 'POST',
success: function (data) {
// Login request was successful
// Do whatever
},
url: '/api/tourist_login',
});
});
Also, you don’t “log in” to RESTful APIs.