Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

3llam's avatar
Level 1

Ajax POST request 500 (Internal Server Error)

hello, I'm building a Laravel website where I can press on favorite button to add a content of p tags tags to favorites table.. when I'm trying to use jQuery to submit the form I'm getting 500 error, you can find below my ajax request and my submit button in my blade :

$("#heart").click(function(e){

	e.preventDefault();
	var quote = document.getElementById('quote').textContent;
	
	$.ajax({
		headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        type: 'post',
        url: '/quotes/favorites',
        data: {
        	'quote':quote,
        },
        dataType: "application/json",
        success: function (data) {
        	alert("success");
        },
	});
});
0 likes
5 replies
Sinnbeck's avatar

What is the error? Open f12 and network. Then make the request, click it in the list and select Preview

1 like
3llam's avatar
Level 1

oh.. I didn't know that I can preview the error thank you for that, I'm a little bit new to this, anyway it looks like it's reaching my controller but I'm not sending any response..

can I resend a variable to my view and handle them in my blade as $variable ?

Sinnbeck's avatar

@3llam maybe show the error and the controller method? Not quite sure what you mean. Add a variable to the ajax request, but set it using blade syntax?

3llam's avatar
Level 1

@Sinnbeck The 500 error is solved but can I return a collection variable that I can use to fill a table in my blade view ?

Sinnbeck's avatar

@3llam yes. Just return it. You can ensure it's converted to json by doing (but it should do so automatically)

return response()->json($data);

Please or to participate in this conversation.