Support the ongoing development of Laravel.io →
Authentication Requests Input
Last updated 2 years ago.
0

If your csrf middleware is enabled then you will need to pass the token as well

0

See http://laravel.io/forum/05-08-2015-posting-using-jquery-revamped. Please atttempt a forum search first a lot of answers already there.
also you are better off using a regular route and then doing the logic in controller using a regular MVC pattern. I just don't like cramming code logic in a route that's just me personally.

Last updated 9 years ago.
0

I don't think his question mentioned posting a form via ajax but rather just posting period. Seems to me he just wants an understanding of why his post is failing instead of a different way to do it.

0

The title is: ajax post in laravel 5 return error 500 (Internal Server Error). Some think inside the box, some say think outside the box, a better solution is to remove the box completely then think. And yes I made that up.

Last updated 9 years ago.
0

teach a man to fish

0

I cannot top that one.

0

I dont want to use a form, I want to directly send a post request and retrieve the response directly too. If I need crf token, how I'm able to generate a crf token for my ajax post request?

0

This should do

<a href="#" id="try" data-link="{{ url('/test') }}" data-token="{{ csrf_token() }}">Try</a>

then include it in the request

$("#try").click(function(){
        var url = $(this).attr("data-link");

        //add it to your data
        var data = {
            _token:$(this).data('token'),
            testdata: 'testdatacontent'
        }


        $.ajax({
            url: "test",
            type:"POST",
            data: data,
            success:function(data){
                alert(data);
            },error:function(){ 
                alert("error!!!!");
            }
        }); //end of ajax
    });

Be sure to look through the helpers.php when you get a chance, tons of useful methods in there

Last updated 9 years ago.
0

What i'm doing in a project is using the csrf_token() in a meta tag.

<meta name="csrf_token" content="{{ csrf_token() }}" />

Then, when I need to make an ajax request, use the beforeSend method.

$("#try").click(function(){
    var url = $(this).attr("data-link");
    $.ajax({
        url: "test",
        type:"POST",
        beforeSend: function (xhr) {
            var token = $('meta[name="csrf_token"]').attr('content');

            if (token) {
                  return xhr.setRequestHeader('X-CSRF-TOKEN', token);
            }
        },
        data: { testdata : 'testdatacontent' },
        success:function(data){
            alert(data);
        },error:function(){ 
            alert("error!!!!");
        }
    }); //end of ajax
});
0

thank you @osiux

works on my copy of Laravel 5.1

0

Thank you somuch @osiux

works fine on Laravel 5.1

i was stuck on chat application because of this issue.

0

@Osiux Perfect Solution for 500 Internal server error for ajax post. Thank You

Last updated 8 years ago.
0

Hi @osiux,

I use laravel 5.4,

i have tag : <meta name="csrf_token" content="{{ csrf_token() }}" />

this is my code :

$.ajax({
url: url + "/test/test", method:'POST', beforeSend: function (xhr) { var token = $('meta[name="csrf-token"]').attr('content');

            if (token) {
                return xhr.setRequestHeader('X-CSRF-TOKEN', token);
            }
        },
            data: { id: number
        },
        success:function(data){
            alert(data);
        },error:function(){ 
            alert("error!!!!");
        }
});

but still 500 Internal server. Please help. Thx

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.