skip to Main Content

I am fetching the data from database using AJAX in laravel. Its working fine. Following is the code of my AJAX:

$('#salesmanlist_input').on('change', function() {
                var salesmanlist_input = $(this).val();
                $.ajax({
                    type: 'GET',
                    url: 'sales/lims_salesman_list',
                    data: {
                        data: salesmanlist_input
                    },
                    success: function(data) {
                        console.log("Data in on change salesman is: " + data);
                        
                    }
                });
            });

the route I’m using for it is as follows

Route::get('sales/lims_salesman_list', 'SaleController@limsSalesmanList')->name('SalesmanList.view');

The problem i’m facing is, its working fine on one blade but not working on other blade. On other file its giving the error as

jquery.min.js:2 GET http://localhost/alimran_final/sales/sales/lims_salesman_list?data=usama

2

Answers


  1. The url url: 'sales/lims_salesman_list', is static. As it will be added to the current url. You should add the url in blade by using the helper route.

    $('#salesmanlist_input').on('change', function() {
                    var salesmanlist_input = $(this).val();
                    $.ajax({
                        type: 'GET',
                        url: "{{ route('SalesmanList.view') }}",
                        data: {
                            data: salesmanlist_input
                        },
                        success: function(data) {
                            console.log("Data in on change salesman is: " + data);
                            
                        }
                    });
                });
    
    Login or Signup to reply.
  2. the Problem You are Facing is that the Route is static,though when you are at /sales url it will also add another sales
    as happened to you


    jquery.min.js:2 GET http://localhost/alimran_final/sales/sales/lims_salesman_list?data=usama
    

    you have 2 solutions:

    1. change url in ajax request from
      url: ‘sales/lims_salesman_list’, to url: ‘/sales/lims_salesman_list’,
      slash added to begining
    2. change url in ajax to url:"{{ route(‘SalesmanList.view’) }}",

    However I prefer to use Solution 2 hence its more dynamic whenever you change lines in web routes file it will dynamically change

    Also try to clear route’s Cache

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search