lewis4u
lewis4u

Reputation: 15047

Ajax alert on validation error

I have made this ajax call:

$.ajax({
    type: myMethod,
    url: myRoute,
    headers: { 'X-CSRF-TOKEN': "{{csrf_token()}}" },
    data: form.serializeArray(),
    dataType: 'json',
    success: function(data){
        console.log('validated!');
    },
    error: function(data) {
        var errors = data.responseJSON;
        for (error in errors) {
            alert(error);
        }
        console.log(errors);
    }
});

I get this response in the console:

enter image description here

And my alerts are those field names:

event_end_date
event_start_date

And what i want is to print those messages:

Endzeitpunkt muss ausgefüllt sein.
Startzeitpunkt muss ausgefüllt sein.

How to get them in alert?

Upvotes: 2

Views: 92

Answers (1)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You could simply use the key error with and index [0] to get the first item :

alert(errors[error][0]);

Hope this helps.

Upvotes: 2

Related Questions