Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass named route parameter with ajax

I need to pass route parameter with ajax but I am using named route method in ajax code.

route I want to go Route

Route::post('/edit/{id}', 'ArticleController@updateArticle')->name('updateArticle');

Ajax

var id= $("input[name=editId]").val();
$.ajax({
   type:'POST',
   enctype: 'multipart/form-data',
   url:"{{ route('updateArticle',"id") }}",
   data: formdata,
   contentType: false,
   processData: false,
   success:function(data){
        $('.alert-success').html(data.success).fadeIn('slow');
        $('.alert-success').delay(3000).fadeOut('slow');
   }
});

I want to use variable id in ajax URL.

like image 485
Arshad Ameen Avatar asked Apr 16 '19 06:04

Arshad Ameen


People also ask

How route is defined in ajax?

Laravel Ajax Form Validation Tutorial so open your routes/web. php file and add the following route. Location:- Root/routes/web. php Route::get('my-form','HomeController@myform'); Route::post('my-form','HomeController@myformPost')->name('my.

How Pass URL in laravel ajax?

How Pass URL in laravel ajax? $. ajax({ url: url, type: 'DELETE', data: { id: userID }, success: function() { alert("YES"); }, error: function() { alert("NO"); } }); I would suggest using Axios instead of jQuery for this. It is a little more expressive / easy to follow / more common.

How to pass javascript variable to laravel route on ajax call?

How to pass javascript variable to laravel route? You can pass javascript variable value to laravel route on ajax call. You have to use a placeholder to generate the URL and after that replace value by using replace method and pass the value to url parameters of ajax method.


1 Answers

I had the same problem, just change your ajax url with this one.

var id= $("input[name=editId]").val();
$.ajax({
   type:'POST',
   enctype: 'multipart/form-data',
   url:"{{ route('updateArticle') }}" + '/' + id,
   data: formdata,
   contentType: false,
   processData: false,
   success:function(data){
        $('.alert-success').html(data.success).fadeIn('slow');
        $('.alert-success').delay(3000).fadeOut('slow');
   }
});
like image 152
Sudaba Solaimankhil Avatar answered Sep 20 '22 17:09

Sudaba Solaimankhil