0

javascript returns a error 'Method not allowed' when it calls to url has parameters.

HTML
1.- <a href="{{ URL::to('acceso', ['option'=>'1']) }}">Acceso</a>
2.- <a href="{{ URL('acceso') }}">Acceso 2</a>

First option return a error. Second option work well.

This is the Route:

Route::get('/acceso/{option?}', function ($option = '') {   
   return view('acceso_Usuario')->with('option', $option);
});

Route::post('call', 'example_Controller@function_example');

This is the Controller:

class example_Controller extends Controller
{
   public function function_example(Request $request)
   {
       if ($request->ajax()) {

           return response()->json([
           ]);

       }
   } 
}

and this is the javascript

$(document).ready(function() {
    var route = "call";
    var token = $("#token").val();

    var parameters=
    {
    };

    $.ajax({
        url: route,
        headers: {'X-CSRF-TOKEN': token},
        type: 'post',
        dataType: 'json',
        data: parameters,
        contentType: 'application/x-www-form-urlencoded',
        success: function (data) {

        },
        error: function (msj) {
            alert("Error Ajax);
        }
    });

 });

someone who can help me! :)

1
  • 1
    Change this contentType: 'application/x-www-form-urlencoded', to contentType: 'application/json', Commented Oct 18, 2018 at 11:38

4 Answers 4

1

Your javascript is making a POST request, but the matching route is defined as allowing only GET,HEAD requests (with Route::get()).

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

4 Comments

he has Route::post('call', 'example_Controller@function_example');,so he should be able to post
@madalinivascu yes, but he stated that the first url is the one with the error, and that doesn't match the 'call' route.
i don't know how the first lines of code are related to the javascript, he is calling an ajax at document ready to a different route, his question makes no sense
I agree, it's a little unclear. However, the error indicates that the route being hit doesn't allow the method being used, and I'm working from the code provided
1

change your ajax url

var url = '{{url("call")}}';

pass csrf token using this way in post method

headers: {

    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

},

Comments

0

Already solved! I explain my problem because I like that the questions are always solved for when other colleagues have the same problem.

The problem is that adding a parameter to the URL changes the root directory of the javascript file. The solution that I have tried is to introduce 'dynamically' the root directory of our website and in this way Ajax will look for the function correctly.

$(document).ready(function() {

   **var currentDirectory = window.location.href;**
   **var rootDirectory = currentDirectory.substring(0, currentDirectory .indexOf("public") + 6);**

   **var route = rootDirectory + "/call";**
   var token = $("#token").val();

   var parameters=
   {
   };

    $.ajax({
    url: route,
    headers: {'X-CSRF-TOKEN': token},
    type: 'post',
    dataType: 'json',
    data: parameters,
    contentType: 'application/x-www-form-urlencoded',
    success: function (data) {

    },
    error: function (msj) {
        alert("Error Ajax);
    }
   });

});

Thank you all for your help! :)

1 Comment

if you had used my answer you shouldn't had that problem because laravel would give you the absolute url to that route
0

change your route into

 var route = '{{url("call")}}';

this will generate a absoulte url to your route

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.