Reputation: 207
Routes.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);
}
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.
But when I call the deactivateState function, it throws a MethodNotAllowedHttpException and I have no idea why.
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
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
Upvotes: 1
Views: 10287
Reputation: 207
@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
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);
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.
Upvotes: 2