Home > Net >  Passing ID to Controller using Ajax - Error 404 in Laravel
Passing ID to Controller using Ajax - Error 404 in Laravel

Time:01-06

I am new to Laravel. Trying to Pass ID from View to Controller but getting Error

POST http://127.0.0.1:8000/getbuffaloidformonitor 404 (Not Found)

This is my View BuffaloMonitor :

$(document).on('click', '.viewmonitormodal', function() {
    var modal_data = $(this).data('info').split(',');
    $('#viewbuffaloID').val(modal_data[1]);
    var buffaloid = document.getElementById('viewbuffaloID').value// get buffalo id from textbox to get data for that ID
    alert(buffaloid);
    //alert(data);
    $(function() {
        $.ajax({
            method : "POST",
            url: "/getbuffaloidformonitor",
            data: {
                '_token': $('input[name=_token]').val(),
                'id': buffaloid,
            },        
            success : function(response) {
                alert(response);
            }
        });
    });
}

This is BuffalomonitorCOntroller :

public function getbuffaloidformonitor(Request $req) {
    $data = buffalodata::find($req->id);
    alert(data);
    $id = $req('data');
    return $id;
}

This Is Route

Route::post('/getbuffaloidformonitor/{id}','App\Http\Controllers\BuffalomonitorController@getbuffaloidformonitor')->name('getbuffaloidformonitor');

CodePudding user response:

Your post route has {id} but it's not necessary. This is what you need Route::post('/getbuffaloidformonitor','App\Http\Controllers\BuffalomonitorController@getbuffaloidformonitor')->name('getbuffaloidformonitor');

CodePudding user response:

pass id to the link http://127.0.0.1:8000/getbuffaloidformonitor

as you write the route

   Route::post('/getbuffaloidformonitor/{id}','App\Http\Controllers\BuffalomonitorController@getbuffaloidformonitor')->name('getbuffaloidformonitor');

CodePudding user response:

You are just pass id by routes Params, so the URL must like this

http://127.0.0.1:8000/getbuffaloidformonitor/yourbuffaloid

You need to change URL.

$.ajax({
   method : "POST",
   url: "/getbuffaloidformonitor/"   buffaloid,
   data: {
      '_token': $('input[name=_token]').val(),
      //'id': buffaloid, remove this line
   },        
   success : function(response) {
      alert(response);
   }
});

If you use this script in you blade template just use

const url = '{{ route("getbuffaloidformonitor",":id") }}'

$.ajax({
    method : "POST",
    url: url.replace(':id',buffaloid),
    data: {
       '_token': $('input[name=_token]').val(),
       //'id': buffaloid, remove this line
    },        
    success : function(response) {
       alert(response);
    }
});

If your routes {id} is optional just

Route::post('/getbuffaloidformonitor/{id?}','App\Http\Controllers\BuffalomonitorController@getbuffaloidformonitor')->name('getbuffaloidformonitor');

with question on your id route you can use both by pass id by route params or you can pass id by data post.

In controller

public function getbuffaloidformonitor(Request $req, $id = null)
{
    // id is get from route params
    $getId = $req->get('id') // this one get from data post.
}
  •  Tags:  
  • Related