Can't find the error in my route in Laravel wi

2019-05-26 16:56发布

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.

5条回答
Deceive 欺骗
2楼-- · 2019-05-26 17:36

try

url:"{{ url('leads/savegroup') }}"

instead of :

url:"{{ route('leads.savegroup') }}"

maybe worked.

查看更多
神经病院院长
3楼-- · 2019-05-26 17:41

Check your route file.The issue may be the same name route is already defined before with other method

查看更多
神经病院院长
4楼-- · 2019-05-26 17:48

You need to setup ajax.setup:

$.ajaxSetup
    ({
        headers:
            {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
    });

and add in HTML this line:

<meta name="csrf-token" content="{{ csrf_token() }}">

because Laravel require the CSRF-TOKEN in every POST request.

查看更多
相关推荐>>
5楼-- · 2019-05-26 17:53

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:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

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 :

Route::group(['prefix' => 'leads'], function () {
     Route::get('/getdata', 'Controller@getdata')->name('leads.getdata');
}

and the second one without group, as following:

Route::get('leads/getdata', 'Controller@getdata')->name('leads.getdata');

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 ;)

查看更多
\"骚年 ilove
6楼-- · 2019-05-26 17:59

Try replacing method:"POST" with _method:"POST"

查看更多
登录 后发表回答