1

in Laravel i want to post simple form data form by using Ajax i get this error:

exception: "Symfony\\Component\\HttpKernel\\Exception\\HttpException"

for example:

View:

<form action="#" method="POST" id="checkPageForAddToSystem">
    {{csrf_field()}}
        <div class="form-group">
            <div class="col-md-12" style="padding: 0px;">
                <input class="form-control" type="text" name="page_name">
            </div>
        </div>
    <div class="text-right">
        <button type="button" class="btn btn-link" data-dismiss="modal">
            close
        </button>
        <button type="submit" class="btn btn-primary">
            add
        </button>
    </div>
</form>

jQuery Ajax:

$('#checkPageForAddToSystem').on('submit', function (e) {
    e.preventDefault();
    let _token = $("input[name='_token']").val();
    $.ajax({
        method: 'POST',
        url: '/panel/addInstagramPageByAjax',
        header: {
            'X-CSRF-TOKEN': _token
        },
        dataType: 'JSON',
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        }
    });
});

Route:

Route::group(['namespace' => 'Dashboard', 'middleware' => ['auth:web'], 'prefix' => 'panel'], function () {
    $this->post('addInstagramPageByAjax', 'ManageInstagramController@addInstagramPageByAjax');
});

Controller Action:

public function addInstagramPageByAjax(Request $request)
{
    return response()->json(['sate' => true]);
}

1 Answer 1

1

Example usage for setting ajax header with CSRF-TOKEN ,

$.ajaxSetup({
     headers: {
           'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
       }
  })

I hope this will help you.

Sign up to request clarification or add additional context in comments.

3 Comments

yes i'm sure thats name is _token, but your helpful code resolve my problem, header couldn't work inside $.ajax
well, Why don't vote this as right answer for the other people.
I had forgotten :) i accept and upvoted your answer, ok? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.