lundi 6 mai 2019

Laravel Ajax results in 419 unknown status response when hosted in AWS EC2

I've this problem with Laravel ajax when hosted in AWS-EC2 resulting in 419 unknown status response.

I've searched and tried almost all solutions given, but the problem still exists.

My meta tag includes CSRF

<meta name="csrf-token" content="">

Below is my laravel ajax code

$(document).ready(function () {
    var BASE_URL = "";
    $("form[name='logForm']").validate({
        rules: {
          log_email: {
            required: true,
            email: true
          },
           log_password: {
            required: true,
            minlength: 5
          },
        },
        messages: {
          log_email: "Please enter a valid email address",
          log_password: "Enter a valid password",
        log_email: "Please enter a valid email address"
        },
        submitHandler: function(form) {
         logForm();
        }
    });
    function logForm() {

        log_email=$("#log_email").val();
        log_password=$("#log_password").val();
        error=0;
        if (log_email == '' && log_password=='') {
            error = 1;
        }
        if (error == 0) {
            $.ajaxSetup({
                // beforeSend: function(xhr, type) {
                //     if (!type.crossDomain) {
                //         xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
                //     }
                // },
                headers: {
                  'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                //url : BASE_URL + '/userlogin',
                url: '/userlogin',
                data: {
                    '_token':  '{!! csrf_token() !!}',
                    'email': $("#log_email").val(),
                    'password': $("#log_password").val()
                },
                dataType: 'json',
                type: 'POST',
                cache: false,
                success: function (response) {
                    if (response.type =='success') {
                        @if(Request::is('page-not-found'))
                         window.location=BASE_URL;
                         @else
                          window.location.reload();   
                         @endif
                    } else if(response.type=='suspended'){
                        $('#log_suspended').show();
                         $('#log_spiner').hide();
                    }
                    else {
                        $('#log_err').show();
                         $('#log_spiner').hide(); 
                        //alert(response.message);
                    }
                },
            });
        }
    }
});

I've commented few lines above in the jquery, those are the solutions i tried.

Tried adding,

contentType: false,
processData: false,

as well, if i add processData, in the console Form-Data its displayed as [object Object]

And below is the Request Headers and Form Data which is taken from console :

Request Headers and Form Data

And in my web.php page i have the following :

Route::post('/userlogin', 'HomeController@login');

The same above code works in existing hosting and in my localhost as well.

I also made sure that the APP_URL and SESSION_DOMAIN in .env to be same.

I can give any more information if needed.

*The question might sound as a Duplicate, but those answers didn't work for me when tried.

Kindly advise.

Thanks

-Vijay



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire