Reputation: 39
In laravel i have a 405 error, butt the strange thing is that when i want to edit some data and click on Update it don't refresh or load the page. when i refresh the page myself the update is set! only i get a error 405 PATCH method not allowed. i have doing some searching on the internet butt can't find how to solve this. things that i have found was like check your apache2 config, add csrf field, make form action post and put {!! method_field('patch') !!} inside that. i have try al lot of things.
someone that can help me with this issue! Thanks al lot
My Route for the project:
Route::resource('project', 'ProjectViewController');
My Controller for the project
public function update(Request $request, $id)
{ DB::table('projects')
->where('id', $id)
->update($request->all()); return redirect('')->with('success', 'Project has been updated');
}
My Jquery Ajax :
$('.modal-footer').on('click', '.edit', function() {
$.ajax({
method: 'PATCH',
url: '{{route('project.update', $project->id)}}',
data: {
'_token': $('input[name=_token]').val(),
'id': $('#fid').val(),
'project_name': $("#pn").val(),
'date': $('#Da').val(),
'location': $('#Lo').val(),
'first_name': $('#FN').val(),
'last_name': $('#LN').val(),
'city_name': $('#CN').val(),
'email': $('#EM').val(),
'number': $('#NUM').val()
},
success: function(data){if(data.success == true){ // if true (1)
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 2000); }},
Upvotes: 2
Views: 1266
Reputation: 480
You need to use POST
as method and you need to define _method=post
your sended data. Please try the following code.
$('.modal-footer').on('click', '.edit', function() {
$.ajax({
method: 'POST',
url: '{{route('project.update', $project->id)}}',
data: {
'_method': 'PUT',
'_token': $('input[name=_token]').val(),
'id': $('#fid').val(),
'project_name': $("#pn").val(),
'date': $('#Da').val(),
'location': $('#Lo').val(),
'first_name': $('#FN').val(),
'last_name': $('#LN').val(),
'city_name': $('#CN').val(),
'email': $('#EM').val(),
'number': $('#NUM').val()
},
success: function(data){if(data.success == true){ // if true (1)
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 2000); }},
Upvotes: 1