Ajax URL

Published 1 week ago by narendra_thapa

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\[email protected]'));

controller part

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

Can you try to use php artisan serve

GhaithAli

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'=>'[email protected]'];

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

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

lancecoder

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

happywheel

Great post. I found your website perfect for my needs. Thanks for sharing this information. I really like your post very much. happy wheels

narendra_thapa

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\[email protected]'));

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()

lancecoder
$(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

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

lancecoder

@narendra_thapa

Try to remove controller

FROM:

Route::get('/getLevel',array('as'=>'getLevel',
            'uses'=>'controller\[email protected]'));

TO:

Route::get('/getLevel',array('as'=>'getLevel',
            'uses'=>'[email protected]'));

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

narendra_thapa

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

lancecoder

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

narendra_thapa

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

lancecoder

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

lancecoder
narendra_thapa

i am not accessing it via ajax, i am just having a problem with it, it shows the GET http://localhost/getLevel?facultyname=BBS 404 (Not Found) error, u can see there in the url path that it is showing in exception which should be http://localhost/project/public/getLevel

lancecoder

Can you try to use php artisan serve

Please sign in or create an account to participate in this conversation.