this is my test ajax in laravel 5 (refer below)
$("#try").click(function(){
var url = $(this).attr("data-link");
$.ajax({
url: "test",
type:"POST",
data: { testdata : 'testdatacontent' },
success:function(data){
alert(data);
},error:function(){
alert("error!!!!");
}
}); //end of ajax
});
and the trigger link
<a href="#" id="try" data-link="{{ url('/test') }}">Try</a>
and my route
Route::post('test', function()
{
return 'Success! ajax in laravel 5';
});
but it gives me error when I run the console in google chrome and it doesnt return the expected response "return 'Success! ajax in laravel 5';"
> POST http://juliver.laravel.com/test 500 (Internal Server Error)
whats wrong/problem to my code? anything im missing? any help, ideas, clues, suggestions, recommendations, help would be greatly appreciated. Thank you!
If your csrf middleware is enabled then you will need to pass the token as well
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.
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.
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.
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?
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
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
});
Thank you somuch @osiux
works fine on Laravel 5.1
i was stuck on chat application because of this issue.
@Osiux Perfect Solution for 500 Internal server error for ajax post. Thank You
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community