Support the ongoing development of Laravel.io →
Input Forms Installation

So I've been struggling with this problem for the past few days. I want to return $name as a json response but when I try to access it in the success function it doesn't give me the wanted result but it gives me undefined.

The Controller code

    	$name = Auth::user()->first_name.Auth::user()->last_name.Auth::user()->id.'.jpg';
        return response()->json(['name' => $name]);

The AJAX code

     success: function (data){
          console.log(data.name);
     }
Last updated 3 years ago.
0

What do you see if you log only data?

0

Object

Result: undefined

0

Can you make a screenshot? I don't understand if the payload is an object or a "result".

0

How do I upload a picture?

0

I'll just do this instead

Last updated 9 years ago.
0

Post here whole ajax request code snippet.

0

it's pretty basic

$.ajax({
            dataType: 'json',
            url: "{{ url('/') }}",
			data: {
				_token: $('input[name=_token]').val()
			},
			success: function (data){
				console.log(data);
			},
)};
Last updated 9 years ago.
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.

© 2025 Laravel.io - All rights reserved.