简体   繁体   中英

how to send a variable for route introduced in ajax in laravel

I have an Ajax function which calls a laravel route and which I need to pass a variable, I have done some things but I didn't spoil it by doing well, let's see if you can help me.

fragment controller where I pass the variable to the view

'presupuesto_id' => $presupuesto->id,

Function Ajax

$("#editar_presupuesto").click(function(){
   

    var url ="{!!route('presupuesto.update',$presupuesto_id)!!}";
    $.ajax({
        type: "POST",
        url: url, // This is what I have updated
        data: $("#form_presupuesto").serialize(),
        success:function(data) {
         
           
            }

            if (data.errors != "" && data.errors != null) {
           
            }
        }
    })
});

use data attribute

your html will be look like this..

<button type="button" id="presupuesto_id" data-route="{{ route('presupuesto.update',$presupuesto_id)}}">Submit</button>

your js will be look like this..

$("#presupuesto_id").click(function(){
   

  let url = $(this).data('route');
  $.ajax({
      type: "POST",
      url: url, // This is what I have updated
      data: $("#form_presupuesto").serialize(),
  })
});

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