Search code examples
javascriptphphtmllaravelcsrf

csrf-token POST 405 (Method Not Allowed) Laravel


I am loading a modal whenever a user tries to register

Blade

<form method="POST" id="registerUser" autocomplete="signupForm-noFill" action={{url("/register")}}>
...
    <div class="form-group text-center" style="">
        <button type="submit" role="button" class="btn btn-hp-modal btn-signup">Sign up</button>
    </div>
</form>

JavaScript

    $("#registerUser").submit(function(e){
        e.preventDefault();
        $.ajax({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            method : 'POST',
            success : function(data){
                $("#legalModal").modal("show");
            }
        });
        return false;
    });

I have it in other pages and it seems to work fine. Any idea what might be wrong in my approach? I just get POST http://example.net/signup 405 (Method Not Allowed) error whenever I Sign Up.


Solution

  • Add url to ajax object:

    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        method : 'POST',
        url: '/register',
        success : function(data){
            $("#legalModal").modal("show");
        }
    });