1

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 
10
  • I don't see any error here. Are you sure you are running the correct function? Commented Feb 22, 2015 at 8:39
  • You might have a conflicting route. Can we see your whole routes file please? Commented Feb 22, 2015 at 11:43
  • @MarcinNabiałek I have edited my question and the error that is thrown is now included. Commented Feb 22, 2015 at 13:17
  • @lukasgeiter I have edited my question and the complete routes is now included. Commented Feb 22, 2015 at 13:18
  • @Rohit Thanks. Can you verify by looking in your browser console that you are indeed making a POST request? Because the error stack says something about GET Commented Feb 22, 2015 at 13:21

1 Answer 1

2

@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.

Sign up to request clarification or add additional context in comments.

1 Comment

So instead of calling an absolute path you were calling a relative path, the code you were running therefor must have been in a subfolder.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.