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

arnabrahman's avatar

Access JSON response in AJAX success function

I've successfully posted a form with AJAX in laravel. After posting the form, controller returns a JSON response

return response()->json([
                "message" => "Success"
            ]);

I want to access this JSON response in AJAX success function.

$.ajax({
            url:route,
            type:'POST',
            dataType: 'json',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },
            data:{rating: rating,review:review},
            success:function(response){
                // Access the JSON response //
            }
        })

How to do it?

0 likes
7 replies
arnabrahman's avatar

@jlrdw my json response

[{"rating":"1","review":"dfdb","book_id":"15"}]

It says undefined when i try to access rating

alert(response.rating);
jlrdw's avatar

In example I mentioned look at this line

 $('#name').text(data.name);
johnathan's avatar

@arnabrahman It looks like your response is an array, does accessing it like below work?

response[0].rating

2 likes
ABDELRHMAN's avatar
success: function(response)
                {
                    alert(response['message']); 
                }

Please or to participate in this conversation.