代码之家  ›  专栏  ›  技术社区  ›  Liga

Laravel Ajax Post方法给出了方法NotallowedHttpException

  •  1
  • Liga  · 技术社区  · 7 年前

    我试图向一个控制器函数发出一个Ajax POST请求,但是我一直收到这个错误。我按照我在网上找到的建议做了补充 $.ajaxSetup 具有 X-CSRF-TOKEN 但还是没有运气。

    “例外”: “symfony\component\httpkernel\exception\methodNotallowedhttpexception”

    PHP

    Route::get('my-controller/mypostfunction', 'MyController@mypostfunction');
    

    mycontroller.php文件

    public function mypostfunction()
    {
        return "Hello poster!";
    }
    

    App.JS

    $( document ).ready(function()
    {
              $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                });
    
              // This alerts the CSRF token correctly!
              alert( $('meta[name="csrf-token"]').attr('content') );
    
        $.post( "my-controller/mypostfunction", function( data ) {
          alert( "Data Loaded: " + data );
        });
    });
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Jignesh Joisar    7 年前

    在web.php文件中,u set get方法,所以只需更改 get 到 post 方法类型

    Route::post('my-controller/mypostfunction', 'MyController@mypostfunction');
    
        2
  •  1
  •   Inzamam Idrees    7 年前

    在你 PHP 更改的路由 得到 到 邮递 这样地:

    Route::get('my-controller/mypostfunction', 'MyController@mypostfunction');
    
    // into 
    
    Route::post('my-controller/mypostfunction', 'MyController@mypostfunction');
    

    我希望这是解决办法。