How to get the url of different forms and validate via javascript

0

I am currently doing so and every time I submit the form the alert returns error even with the fields filled in correctly

$('.form-ajax').on('submit', function(e) {
        e.preventDefault();

        var form = $('#formnews');

        $.ajax({
            method: 'POST',
            url: form.attr('action'),
            async: true,
            data: form.serialize(),
            cache: false,
            success: function(result) {
                if (result.success) {
                    swal('Bom trabalho!', 'Sua inscrição foi efetuada com sucesso!', 'success');
                    $('#formnews').val('');
                } else {
                    swal('Alguma coisa errada', result.message, 'warning');
                }
            },
            error: function(data) {
                swal('Erro!', 'Ocorreu um erro', 'error');
            }
        })
    });
<form action="{{ route('newsletter.post.ajax') }}" method="POST" id="formnews">
                    {{ csrf_field() }}
                    <div class="field has-addons">
                        <div class="control is-expanded has-icons-left has-icons-right">
                            <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                                <p class="control is-expanded has-icons-left has-icons-right">
                                    <input id="email_newsletter" type="email_newsletter" class="input" name="email_newsletter" value="{{ old('email_newsletter') }}" placeholder="Seu melhor email" required>
                                        @if ($errors->has('email'))
                                            <span class="help-block">
                                                <strong>{{ $errors->first('email') }}</strong>
                                            </span>
                                        @endif
                                    <span class="icon is-small is-left">
                                            <i class="fa fa-envelope"></i>
                                    </span>
                                    <span class="icon is-small is-right">
                                             <i class="fa fa-check"></i>
                                    </span>
                                </p>
                            </div>
                        </div>
                        <div class="control">
                            <button type="submit" class="button is-warning">
                                Cadastrar
                            </button>
                        </div>
                    </div>
               </form>
    
asked by anonymous 05.10.2017 / 22:43

1 answer

1

Well, without the complete code I can just make assumptions, but at first I think you might be in the if test (result.success).

How do you validate the fields code? For result to be equal to TRUE you should determine some tests, however, they do not seem to occur at any time.

So, I think the code will always return error even with the fields filled in correctly.

    
05.10.2017 / 22:53