简体   繁体   中英

route not found on ajax post

I'm trying to create a dependent select using ajax, here is my JS

$("#make").change(function(){
    $.ajax({
        url: "{{ url('chauffeur/ajax_vehicle_model') }}?make=" + $(this).val(),
        method: 'GET',
        success: function(data) {
            $('#model').html(data.html);
        }
    });
});

My routing looks like this

Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
    Route::get('chauffeur/ajax_vehicle_model','Admin\ChauffeurController@get_vehicle_model');
});

And in my controller I have this

public function get_vehicle_model(Request $request)
{
    ....
}

But I get a 404 error, any idea what I'm doing wrong here?

Try to change your route like this

routing file

Route::get('chauffeur/ajax_vehicle_model', ['as'=> 'chauffeur.ajax.vehicle',  'uses' => 'Admin\ChauffeurController@get_vehicle_model']);

Now your js code should be like this (if your js code is in .blade.php file)

$("#make").change(function(){
    $.ajax({
        url: "{{ route('chauffeur.ajax.vehicle') }}?make=" + $(this).val(),
        method: 'GET',
        success: function(data) {
            $('#model').html(data.html);
        }
    });
});

try this.

As you see, you have an argument in your route group prefix , with the value admin .

This prefixes your routes inside that route group with admin . This way, your url in JS should look like:

url(“admin/chauffeur/ajax_vehicle_model”)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM