php - Laravel 5 中的 MethodNotAllowedHttpException

标签 php http laravel laravel-5

路由.php

Route::post('deactivatecountry', array(
            'as'   => 'deactivatecountry', 
            'uses' => 'CountriesController@deactivateCountry'
));
Route::post('deactivatestate', array(
            'as'   => 'deactivatestate', 
            'uses' => 'StatesController@deactivateState'
));

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);
}

当调用我的 javascript 中的 deactivateCountry 函数时,代码运行良好。它调用 CountriesController 中的 deactivateCountry 方法,并按预期停用该国家/地区。

但是当我调用 deactivateState 函数时,它会抛出 MethodNotAllowedHttpException,我不知道为什么。

国家和州的路线相同,功能相似,但一个在工作,另一个在不工作。

有人可以帮我解决这个问题吗?我已经坐了几个小时了,我无法弄清楚为什么会这样

这是我在调用 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

最佳答案

@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);
}

我在路由前面加了一个/。所以在 xmlhttp.open 中,我将它从 xmlhttp.open("POST","deactivatestate", true); 更改为 xmlhttp.open("POST","/deactivatestate", true) ;

当我检查 xmlhttp 请求的 POST url 时,我到达了这个位置,我与我指定的不同,这就是 Laravel 假设它是 GET 请求而不是 POST 的原因。

关于php - Laravel 5 中的 MethodNotAllowedHttpException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655210/

相关文章:

php - 如何在 Eloquent Orm 中实现自引用(parent_id)模型

php - symfony 2 表单字符串缺少第一个字符

.htaccess - Laravel 5.2 - 使用 .htpasswd 的基本身份验证

php - Laravel 5,Behat,Mink : base_url parameter is not read from behat. yml

PHP stream_context_create 和 HTTP header - 数组或字符串,\r\n 最后,Content-Length 可选?

java - 如何重用 HttpUrlConnection?

flash - SWF 闪存文件未加载 IIS 压缩

php - MySQL 检查重复条目的更好方法

php - Laravel 验证错误

php - WordPress 中自定义 wp_query 的分页需要 404 错误页面