I'm using Laravel 5.6, and I have an error :
Whoops, looks like something went wrong. (1/1) MethodNotAllowedHttpException
and the following is my view (leads/show.blade.php):
<form method="post" id="student_form">
{{csrf_field()}}
<span id="form_output"></span>
<div class="form-group">
<label>Choose Group for Your Lead</label>
<select name="group_id" id="group_id" class="form-control">
@foreach($groups as $group)
<option value="{{$group->id}}"> {{$group->name}}</option>
@endforeach
</select>
<input type="hidden" name="customer_id" id="customer_id" value="{{$lead->id}}">
</div>
<div class="modal-footer">
<input type="hidden" name="student_id" id="student_id" value="" />
<input type="hidden" name="button_action" id="button_action" value="insert" />
<input type="submit" name="submit" id="action" value="Add" class="btn btn-info" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
- Ajax request and response:
<script type="text/javascript"> $(document).ready(function() { $('#student_form').on('submit', function(event){ event.preventDefault(); var form_data = $(this).serialize(); $.ajax({ url:"{{ route('leads.savegroup') }}", method:"POST", data:form_data, dataType:"json", success:function(data) { if(data.error.length > 0) { var error_html = ''; for(var count = 0; count < data.error.length; count++) { error_html += '<div class="alert alert-danger">'+data.error[count]+'</div>'; } $('#form_output').html(error_html); } else { $('#form_output').html(data.success); $('#student_form')[0].reset(); $('#action').val('Add'); $('.modal-title').text('Add Data'); $('#button_action').val('insert'); } } }) }); }); </script>
and the route is :
Route::post('leads/savegroup', 'LeadsController@savegroup')->name('leads.savegroup');
Please help me to find the error.
try
instead of :
maybe worked.
Check your route file.The issue may be the same name route is already defined before with other method
You need to setup ajax.setup:
and add in HTML this line:
because Laravel require the CSRF-TOKEN in every POST request.
Thanks for your help
Actually I solved my problem, where the problem was in the route as well.
But let me start from the beginning, what error stages I faced and how to solve it:
first: I tried to remove Ajax, and run the form as normal with action="...", once I ensure that the form working well, so I move to next stage, to check Ajax.
Second: When I start to check Ajax, I found that Ajax is working well, but the problem, but the error still shown:
So I open "laravel.log" in my application, and I checked the latest errors I have in my application, I found that the error maybe came from DB (SQL) or from the route as well. So I moved to check the controller, and I ensure that there is no any error in route as well, because I already use it before in other pages and it works very well.
So the last chance for me to check the route, and the problem should be in route as well. I checked my route as well, and after many time on changing the routes names and etc. I noticed that I make it in 2 groups:
The first group :
and the second one without group, as following:
So this is my problem as well. Once I moved the route from outside to inside the group > It working well and the problem solved as well.
So at the end of the day, the problem in route as well.
Thanks for yr help ;)
Try replacing method:"POST" with _method:"POST"