how to pass named route parameter with ajax
15,716
Solution 1
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');
}
});
Solution 2
Try to use replace function:
var id = $("input[name=editId]").val();
var url = "{{ route('updateArticle', ":id") }}";
url = url.replace(':id', id);
$.ajax({
type:'POST',
enctype: 'multipart/form-data',
url: url,
data: formdata,
contentType: false,
processData: false,
success:function(data){
$('.alert-success').html(data.success).fadeIn('slow');
$('.alert-success').delay(3000).fadeOut('slow');
}
});
Solution 3
Put +
around id
variable and make sure you are passing X-CSRF-Token via formdata
variable or try sending manualy :
replace this line :
url:"{{ route('updateArticle',"id") }}",
with this :
url:"{{ route('updateArticle',"+id+") }}",
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');
}
});
Related videos on Youtube
![Arshad Ameen](https://lh3.googleusercontent.com/-KODe-tYI9D4/AAAAAAAAAAI/AAAAAAAAAHY/xVU31piQZ3M/photo.jpg?sz=256)
Author by
Arshad Ameen
Updated on September 15, 2022Comments
-
Arshad Ameen almost 2 years
I need to pass route parameter with
ajax
but I am using named route method inajax
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
inajax
URL. -
Arshad Ameen about 5 yearsbut the id is dynamic
-
A.A Noman over 2 yearsIt's really awesome
-
aliencity over 2 yearsMan you're a life saver