【问题标题】:Why method not allowed http exception?为什么方法不允许http异常?
【发布时间】:2018-05-28 09:31:10
【问题描述】:

我在 laravel 中有一个表单。

我想使用 ajax 发布请求将数据发送到服务器。

laravel 给我错误。不知道为什么?

My view source url is  :  http://localhost/lily/public/search

(1/1) MethodNotAllowedHttpException

在 RouteCollection.php(第 251 行)中

在 RouteCollection->methodNotAllowed(array('GET', 'HEAD'))

在 RouteCollection.php(第 238 行)中

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
        crossorigin="anonymous"></script>
    </head>
    <body>
        <script type="text/javascript">
            $(document).ready( function () {
                $("#submit").submit( function(){
                    var name = $("#name").val();
                    console.log(name);
                    $.ajax({
                        type: "POST",
                        url :  "{{url('/search')}}",
                        data : name ,
                        success : function(data)
                        {
                            console.log(data)
                        },

                        error : function(error)
                        {
                            console.log(error)
                        } 
                    });
                });
            });
        </script>

        <div class="col-md-6 offset-3">
            <form id="submit" method="POST">
                <input type="name" name="name" id="name">
                <button type="submit" class="btn btn-success">search</button>
            </form>
        </div>
    </body>
</html>

Route::post('/search/{name}', 'HomeController@in');

public function in() {
    return json("fdfdfdfdfdf");
}

【问题讨论】:

  • 您正在使用的 METHOD 是否存在路由。发布,放置,删除等?另外,您是否启用了 CSRF 保护?您需要通过令牌。 laravel.com/docs/5.5/csrf#csrf-x-csrf-token
  • 生成的表单动作的实际内容是什么?
  • 这将给出 TokenMismatchException
  • 你能给我一个从视图到控制器的简单工作示例吗?

标签: javascript laravel


【解决方案1】:

您为 /search/parameter 定义了一个路由,但您的操作只是“/search”。

删除路由中无用的 {name} 部分。或者使用 {name?}

使其成为可选

【讨论】:

    【解决方案2】:

    在将路由定义更改为删除 {name} 或将其设为可选 {name?} 之后,将 CSRF 令牌与请求一起传递,然后将 data 更改为

    $(document).ready(function() {
    
        $("#submit").submit(function() {
    
            e.preventDefault(); // Prevent the Default Form Submission
            var name = $("#name").val();
            console.log(name);
    
            $.ajax({
                type: "POST",
                url: "{{ url('/search') }}",
                data: {
                    name: name,
                    _token: "{{ csrf_token() }}"
                },
                success: function(data) {
                    console.log(data)
                },
                error: function(error) {
                    console.log(error)
                }
            });
        });
    
    });
    

    输入类型应为text,而不是表单中的name

    <input type="text" name="name" id="name">
    

    【讨论】:

    • 你能给我一个从视图到控制器的简单工作示例吗
    【解决方案3】:

    因为您没有将 CSRF 令牌值添加为隐藏类型。

    您必须将 CSRF 隐藏输入添加到表单。

    <input type="hidden" name="_token" value="{{ csrf_token() }}" />
    

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 2021-02-02
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 2011-01-13
      • 1970-01-01
      相关资源
      最近更新 更多