【问题标题】:Laravel 5.1: MethodNotAllowedHttpExceptionLaravel 5.1:MethodNotAllowedHttpException
【发布时间】:2015-11-08 22:26:37
【问题描述】:

当我从 Laravel 中的表单发出请求以将其存储在数据库中时,它会抛出此错误:MethodNotAllowedHttpException

我不明白我做错了什么,请你帮帮我吗?

这是我的表格:

<form method="POST" action="/admin/ajax/edit">
        {{ csrf_field() }}
        <div class="textEdit">
            <div class="marginizer"> 
                <textarea id="edit" name="edit"></textarea>
            </div>
        </div>
        <input type="submit">
    </form>

这是我的路线文件:

Route::get('admin/dashboard', 'Dashboard@index');
Route::get('admin/dashboard/{id}', 'Dashboard@show');
Route::get('admin/dashboard/edit/{site}', 'Edit@edit');
Route::get('admin', 'Dashboard@index');

// Register and Login routes...
Route::get('admin/login', 'Login@index');
Route::get('admin/register', 'Register@index');

// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

// Ajax routes
Route::post('admin/ajax/edit', 'EditAjax@store');

// UI routes
Route::get('/', 'ThemeLoader@index');
Route::get('/{site}', 'ThemeLoader@show');

Route::get('migrate', 'migrate@migrate');
Route::get('migrate/refresh', 'migrate@refresh');

这是处理请求的控制器:

public function store(Request $request) {

    $content = new Content;

    $content->site = 'Index';
    $content->block = 1;
    $content->content = $request->input('edit');
    $content->active = 1;

    $content->save();
}

它实际上将数据正确存储在数据库中,但它继续出现错误。

【问题讨论】:

  • 也许你的路线不正确?尝试将您的操作切换为 `action="{{ url('admin/ajax/edit') }}"
  • 你能发布你的ajax调用吗
  • 我没有使用 ajax,我以前用过但现在不用了
  • 如果是这种情况,那么您将需要在您的 store 方法中添加一个 return.. 并且不再将其称为 ajax 可能是有意义的

标签: php database forms laravel-5.1


【解决方案1】:

好吧,你有没有通过php artisan route:list查看你的路线? 有时当我摆弄路由时,我得到了 MethodNotAllowedHttpException 并且不得不清除它的缓存。

编辑正如科菲指出的那样。如果您的 laravel 项目目录不是 htdocs 的直接子目录,则有可能。所以,不如试试{{ route('admin/ajax/edit') }}

【讨论】:

    【解决方案2】:

    试试这个:

    <form method="POST" action="{{ route('admin/ajax/edit') }}">
    

    【讨论】:

      【解决方案3】:

      MethodNotAllowedHttpException
      通常发生在您尝试对路由使用 HTTP 方法但尚未在路由文件中定义该方法时。例如,如果你 POST 到一个方法,但你只在 routes.php 中为路由定义了一个 GET 方法。

      【讨论】:

        【解决方案4】:

        试试这个代码,我想它可以帮助你:

        <?php 
            {{ Form::open(array('action' => 'YourController@YourAction', 'method' => 'post')) }}
            {{ csrf_field() }}
                "..Your Form Inputs.."
            {{ Form::close() }}
        ?>
        

        【讨论】:

          猜你喜欢
          • 2015-10-28
          • 2017-09-23
          • 2018-03-30
          • 1970-01-01
          • 2016-01-15
          • 1970-01-01
          • 2017-03-13
          • 1970-01-01
          • 2019-04-15
          相关资源
          最近更新 更多