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

narendra_thapa's avatar

Ajax URL

i am trying to use ajax for dynamic change of dropdown box but URL is not directing to the controller function

ajax function

          $.ajax({
            type:'get',
            url:"{{URL::route('getLevel')}}",
            data:{'facultyname':facultyname},
            datatype:'html',
            success:function(data){
                console.log('success');
                console.log(data.msg);
                },
            error:function(){
            }
            })

route part

    Route::get('/getLevel',array('as'=>'getLevel',
    'uses'=>'controller\manageAdminController@getLevel'));

controller part

    public function getLevel(Request $request){
    echo "getlevel";    
    return response()->json(['msg','this is message from ajax']);
}
0 likes
65 replies
GhaithAli's avatar

Here is an example from my app to do a ajax get request:

var getLink = '/my-get-link';
var getData={ 
    _token: "{{ csrf_token() }}",
    key: value
}
$.get(getLink, getData, function (response) {
                    var jsonResponse = $.parseJSON(response);
                    if (jsonResponse.error == 'OK') {
                        //do something with the data.
                    }
                })

My Router (web.php) looks like this:

Route::get('/my-get-link',['uses'=>'SomeController@someFunction'];

My Controller Part:

class SomeController extends Controller{
    public function someFunction(Request $request){
        //some query
        //You can use the dd() function to "dump and die" some data.
        dd("we are here")
        return $data;
    }
}

Hope this helps.

narendra_thapa's avatar

thanks for ur response GhaithAli but it didn't work for me i am having a problem with my url, when i see my url by printing it on my console it is printing the given url as string like

'/getLevel'

it isnot taking a full url path, so help me to figure it out

ardnor's avatar

Can you share your screenshot of your page and the process? Thanks.

narendra_thapa's avatar

ajax.js file

                   $(function(){
                    $('#faculty-list').change(function(){
                 var facultyname=$('#faculty-list :selected').val();
                  alert(facultyname);
                 var url='/getLevel';
                  console.log(url);                 
                           $.ajax({
                    type:'get',
                    url:url,                
                    data:{'facultyname':facultyname},               
                    datatype:'json',
                    success:function(data){
                console.log('success');
                console.log(data.msg);
                },
                error:function(){
                }
            })      
                  });
                });

web.php file

            Route::get('/getLevel',array('as'=>'getLevel',
            'uses'=>'controller\manageAdminController@getLevel'));

manageAdminController.php file

                               public function getLevel(Request $request){
                               echo "getlevel";
                                  return response()->json(['msg','this is message from ajax']);
                               }

problem here is i am not getting the url localhost/projectname/public/getLevel in my console, so it is not directing on controller function getLevel()

ardnor's avatar
$(function(){
                    $('#faculty-list').change(function(){
                 var facultyname=$('#faculty-list :selected').val();
                  alert(facultyname);
                 var url='/getLevel';
                  console.log(url);                 
                           $.ajax({
                    type:'get',
                    url:url,                
                    data:{'facultyname':facultyname},               
                    datatype:'json',
                    success:function(data){
                console.log('success');
                console.log(data.msg);
                },
                error:function(){
                }
            })      
                  });
                });

About the code above what console do you expect to display?

  1. console.log(url);
  2. console.log('success');
  3. console.log(data.msg);

Because in your code it will access your method "getLevel" every time you on change your select options via ajax and it will display the content of your "getLevel" method block from the "manageAdminController" controller.

narendra_thapa's avatar

i want to dispaly url

i am checking the URL because it is not directing me to controller, that's why i checked my url by printing it on my console and i found that it is giving /getlevel instead of giving me a localhost/project/public/getLevel

i need to execute controller function getLevel() but i am not able to do it, so i think i have a problem with my url part

ardnor's avatar

@narendra_thapa

Try to remove controller

FROM:

Route::get('/getLevel',array('as'=>'getLevel',
            'uses'=>'controller\manageAdminController@getLevel'));

TO:

Route::get('/getLevel',array('as'=>'getLevel',
            'uses'=>'manageAdminController@getLevel'));

You don't have any changes of folder structure inside Http folders, right?

narendra_thapa's avatar

i have my manageAdminController file in app/Http/Controllers/controller and it isnot a problem in route

the main problem is in the url that i have written in ajax function while i have written the url='/getlLevel' it should have print localhost/project/public/getLevel in console but it prints /getLevel only, so i need to fix the problem on ajax url part

ardnor's avatar

Why do you need to print the whole path of that route?

narendra_thapa's avatar

i don't need it, i am just finding out my problem that why it is not directing to my controller function, help me to sort out this problem

ardnor's avatar

But you can access your controller like "getLevel" method via ajax?

ardnor's avatar
ardnor
Best Answer
Level 6

Can you try to use php artisan serve

1 like
ardnor's avatar

So that you just hit localhost:8000 in the browser

ardnor's avatar

Can you show the screenshot of your code and the file structure?

ardnor's avatar

I can't see your manageAdminController inside Http/Controllers

ardnor's avatar

Okay. It was a different project. Can you show the controller codes as well as the routes?

ardnor's avatar

Did you run the php artisan serve?

ardnor's avatar

yes because your route in ajax is just "/getLevel" so it will point automatically in your hostname which is the localhost or you create a local domain and it could return as localhost/getLevel

If you don't want to use php artisan serve then you should add "/projectname/public/getLevel" manually. But take note if you deploy it to your hosting which you have a domain then it could be a pain to you because you need to change it manually like I said in the first paragraph.

ardnor's avatar

Then what happens after you run it?

ardnor's avatar

I think you are confused if you are accessing the route via ajax you can see it through the network if response correctly. Please click the Network after Sources and see me the screenshot.

It seems you don't have jquery installed on your page.

Next

Please or to participate in this conversation.