Heroku上的Laravel - MethodNotAllowedHttpException

时间:2017-05-15 06:13:16

标签: ajax laravel heroku

我想知道如何获得此MethodNotAllowedHttpException。我一直在尝试将我的应用程序部署到heroku,并且我一直收到此错误。我有一些POST路线:

路由/ web.php:

Route::get  ('/', 'CodeController@index');

Route::get  ('/api/token/',         'CodeController@token');
Route::post ('/api/checkpassword/', 'CodeController@checkPassword');
Route::post ('/api/setpassword/',   'CodeController@setPassword');
Route::post ('/api/clearpassword/', 'CodeController@clearPassword');
Route::post ('/api/changeuri/',     'CodeController@changeUri');

Route::post ('/api/submission/', 'SubmissionController@submit');
Route::post ('/api/convert/',    'SubmissionController@convert');

Route::post ('/api/postresult/', 'CodeController@postResult');
Route::post ('/api/postdata/',   'CodeController@postData');
Route::get  ('/api/getdata/',    'CodeController@getData');

Route::get  ('/share/{uri}', 'CodeController@share');
Route::get  ('/{uri}',       'CodeController@open');

在我的javascript文件中,我通过ajax方法访问它。以下是我的ajax方法之一:

$.ajax ({
    url: '/api/checkpassword/',
    type: 'POST',
    data: {
        "uri": uri,
        "password": $('#password').val ()
    },
    success: function (data) {
        if (data['status'] == 'success')
            window.location.reload (true);
        else {
            $('#checkpassword-alert').html (data['status']);
            $('#checkpassword-alert').show ();
        }
    },
    error: function (jqXHR, textStatus, errorThrown) {
        $('#checkpassword-alert').html (errorThrown);
        $('#checkpassword-alert').show ();
    }
});

在我的本地机器上一切正常。它收到了适当的回复。但是,当我部署到heroku时,它会进入错误功能,并在Method Not Allowed中显示#checkpassword-alert

不知怎的,我可以让路线工作(但仍然不如预期)。我将所有POST路由更改(或添加)到GET,并将所有ajax方法(类型)更改为'GET',错误消失了。但是我的控制器出现了另一个错误。它告诉我一些params('uri')未设置(我检查它是否设置在控制器中)。

这是我如何传递MethodNotAllowedHttpException:

Route::get  ('/', 'CodeController@index');

Route::get  ('/api/token/',         'CodeController@token');

// or just simply change these POST routes to GET
Route::post ('/api/checkpassword/', 'CodeController@checkPassword');
Route::post ('/api/setpassword/',   'CodeController@setPassword');
Route::post ('/api/clearpassword/', 'CodeController@clearPassword');
Route::post ('/api/changeuri/',     'CodeController@changeUri');

Route::post ('/api/submission/', 'SubmissionController@submit');
Route::post ('/api/convert/',    'SubmissionController@convert');

Route::post ('/api/postresult/', 'CodeController@postResult');
Route::post ('/api/postdata/',   'CodeController@postData');
Route::get  ('/api/getdata/',    'CodeController@getData');

Route::get  ('/share/{uri}', 'CodeController@share');
Route::get  ('/{uri}',       'CodeController@open');

Route::get ('/api/checkpassword/', 'CodeController@checkPassword');
Route::get ('/api/setpassword/',   'CodeController@setPassword');
Route::get ('/api/clearpassword/', 'CodeController@clearPassword');
Route::get ('/api/changeuri/',     'CodeController@changeUri');

Route::get ('/api/submission/', 'SubmissionController@submit');
Route::get ('/api/convert/',    'SubmissionController@convert');

Route::get ('/api/postresult/', 'CodeController@postResult');
Route::get ('/api/postdata/',   'CodeController@postData');

我无法弄清楚当部署它时路由怎么不起作用。有人能帮助我吗?

0 个答案:

没有答案