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

amk's avatar
Level 4

Uncaught ReferenceError in Ajax get method

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(document).on('change','#maincatg_id', function() {
            var mainID=$(this).val();
            $.ajax({
                type:'get',
                url:"{!!URL::to('maincatg/ajax')!!}",
                data:{'id':mainID},
                success:function(response){
                    console.log(data);
                    //console.log(data);
                    // console.log(data.length);
                },
                error:function(){
                    console.log('error');
                }
            });
            
        });
    }); 
</script>

and route is..

Route::get('maincatg/ajax','ProductController@maincatgAjax');

Controller...

public function maincatgAjax(Request $request)
    {

         $data = Category::select('name','id')->where("maincatg_id",$request->id)->get();
         return response()->json($data);

    }

my error show like that.. Uncaught ReferenceError: data is not defined at Object.success (create:208) at i (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at A (jquery.min.js:4) at XMLHttpRequest. (jquery.min.js:4)

0 likes
7 replies
topvillas's avatar

The success callback has no idea what data is. Try constructing the data object you send with the request outside the call and reference that.

var data = {'id': $(this).val()};

$.ajax({
    type: 'get',
    url: "{!!URL::to('maincatg/ajax')!!}",
    data: data,
    success: function(response) {
            console.log(data);
    },
})

I think that'll work.

1 like
amk's avatar
Level 4

It can not work sir.... I want callback 'data' from controller but it can not return it with json.

public function maincatgAjax(Request $request)
    {

         $data = Category::select('name','id')->where("maincatg_id",$request->id)->get();
         return response()->json($data);

    }

can you show me way,pls sir....

Cronix's avatar

try

return response()->json(['data' => $data]);

and then

console.log(response.data);
1 like
amk's avatar
Level 4

It can't work sir... I'm tired this week bez of this one error.. here it show error like that........

create:209 Uncaught ReferenceError: response is not defined at Object.success (create:209) at i (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at A (jquery.min.js:4) at XMLHttpRequest. (jquery.min.js:4)

rawilk's avatar
rawilk
Best Answer
Level 47

@amk All you needed to change was console.log(data); to console.log(response);.

1 like
amk's avatar
Level 4

thank @wilk_randall But.. when I check this code.

console.log('success');
console.log(response.length);

it can work but it show only success. it console output length is 0.Why?

Also I have been check Category database.Data already have.

 $data = Category::select('name','id')->where("maincatg_id",$request->id)->get();
 return response()->json($data);

I think json have some problem.

rawilk's avatar

I'm not sure why your second console log wouldn't be working, I'd need to see the error. When I return json responses from my controllers, I usually return a keyed array, so in your case I'd return something like return response()->json(['categories' => $data]);

If you return it like that, in your success handler, you should be able to console log your results like this:


$.ajax({
    ...
    success: function (response) {
        console.log(response.categories);
    }
});

1 like

Please or to participate in this conversation.