Laravel 5 中的 MethodNotAllowedHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28655210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MethodNotAllowedHttpException in Laravel 5
提问by Rohit
Routes.php
路由.php
Route::post('deactivatecountry', array(
'as' => 'deactivatecountry',
'uses' => 'CountriesController@deactivateCountry'
));
Route::post('deactivatestate', array(
'as' => 'deactivatestate',
'uses' => 'StatesController@deactivateState'
));
Javascript
Javascript
function deactivateCountry(country_id) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {}
}
xmlhttp.open("POST","deactivatecountry", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("country_id=" + country_id);
}
function deactivateState(state_id) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {}
}
xmlhttp.open("POST","deactivatestate", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("state_id=" + state_id);
}
When the deactivateCountry function in my javascript is called, the code works perfectly fine. It calls the deactivateCountry method in the CountriesController and it deactivates the country as expected.
当我的 javascript 中的deactivateCountry 函数被调用时,代码运行良好。它调用CountriesController 中的deactivateCountry 方法并按预期停用该国家/地区。
But when I call the deactivateState function, it throws a MethodNotAllowedHttpException and I have no idea why.
但是当我调用deactivateState 函数时,它抛出一个MethodNotAllowedHttpException,我不知道为什么。
Both the country and the state have the same route and the functions are similar but one is working and the other is not.
国家和国家的路线相同,功能相似,但一个工作,一个不工作。
Can somebody please help me with this? I have been sitting on this for a couple of hours now and I'm not able to figure out why this is happening
有人可以帮我解决这个问题吗?我已经坐了几个小时了,但我无法弄清楚为什么会这样
Here is the error that I am getting when I call the deactiveState method
这是我调用 deactiveState 方法时遇到的错误
1/1
MethodNotAllowedHttpException in RouteCollection.php line 207:
in RouteCollection.php line 207
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 194
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 142
at RouteCollection->match(object(Request)) in Router.php line 716
at Router->findRoute(object(Request)) in Router.php line 642
at Router->dispatchToRoute(object(Request)) in Router.php line 618
at Router->dispatch(object(Request)) in Kernel.php line 160
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 111
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53
回答by Rohit
@MarcinNabia?ek and @lukasgeiter. Thank you both for your time and patience with this question. I have been able to solve the problem. The change I made was in the javascript
@MarcinNabia?ek 和 @lukasgeiter。感谢您抽出时间和耐心回答这个问题。我已经能够解决这个问题。我所做的更改是在javascript 中
function deactivateCountry(country_id) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {}
}
xmlhttp.open("POST","/deactivatecountry", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("country_id=" + country_id);
}
function deactivateState(state_id) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {}
}
xmlhttp.open("POST","/deactivatestate", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("state_id=" + state_id);
}
I added a / in front of the route. So in xmlhttp.open I changed it from xmlhttp.open("POST","deactivatestate", true);
to xmlhttp.open("POST","/deactivatestate", true);
我在路线前面加了一个 / 。所以在 xmlhttp.open 我把它从xmlhttp.open("POST","deactivatestate", true);
改为xmlhttp.open("POST","/deactivatestate", true);
I got to this position when I checked the POST url which xmlhttp was requesting and i was different from the one I had specified which is why Laravel was assuming that it was a GET request instead of a POST.
当我检查 xmlhttp 请求的 POST url 时我到达了这个位置,我与我指定的那个不同,这就是为什么 Laravel 假设它是一个 GET 请求而不是一个 POST 请求。