使用Ajax在Laravel中找不到我的路线中的错误 [英] Can't find the error in my route in Laravel with Ajax

查看:88
本文介绍了使用Ajax在Laravel中找不到我的路线中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.6,但出现错误:

I'm using Laravel 5.6, and I have an error :

哎呀,看起来好像出了点问题. (1/1) MethodNotAllowedHttpException

Whoops, looks like something went wrong. (1/1) MethodNotAllowedHttpException


以下是我的观点(leads/show.blade.php):


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请求和响应:
  • <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>
    

    ,路线是:

    Route::post('leads/savegroup', 'LeadsController@savegroup')->name('leads.savegroup');
    

    请帮助我找到错误.

    推荐答案

    感谢您的帮助

    实际上,我也解决了我的问题,问题也出在路线上.

    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:

    首先:我尝试删除Ajax,并在确保表单正常运行后使用action ="..."正常运行该表单,因此我进入下一阶段来检查Ajax.

    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.

    第二:当我开始检查Ajax时,我发现Ajax运行良好,但是出现了问题,但错误仍然显示:

    Second: When I start to check Ajax, I found that Ajax is working well, but the problem, but the error still shown:

    无法加载资源:服务器响应状态为500 (内部服务器错误)

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

    因此,我在应用程序中打开"laravel.log",并检查了应用程序中的最新错误,发现错误可能来自DB(SQL)或路由.因此,我开始检查控制器,并确保路由中也没有任何错误,因为我之前在其他页面中已经使用过它,并且效果很好.

    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:

    第一组:

    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.

    感谢您的帮助;)

    这篇关于使用Ajax在Laravel中找不到我的路线中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆