14

I am trying to issue a simple AJAX request to populate a menu in Laravel, however, I am having a lot of trouble with getting it to work properly.

I am not sure what the issue is, and after a couple hours of searching, I cannot find anything that can help.

Here is my AJAX request:

$.ajax({
            type: 'POST',
            url: '/ajax/populateApiAuth',
            data: json,
            dataType: 'JSON',
            success: function (json) {
                alert('test');
                return true;
            },
            error: alert('fail')
});

My route to the AJAX callback:

Route::get('/ajax/populateApiAuth', 'ApiController@populateApiAuth');

and my controller to handle the AJAX callback in ApiController:

public function populateApiAuth()
    {
        return Response::json(array('msg' => 'test');
    }

When sending the AJAX request, it returns with the fail message in the error parameters, and in the console, it tells me:

POST http://localhost:8000/ajax/populateApiAuth 405 (Method Not Allowed) 

Researching this error message, it results from making a POST request to a different domain/server? How can this be?

I have tried to use an absolute URL for the AJAX request with:

url: '{{ URL::to("ajax/populateApiAuth") }}

which gives the full URL: http://localhost:8000/ajax/populateApiAuth but that does not solve the issue either.

1 Answer 1

48

Wouldn't this be your issue?

Route::get('/ajax/populateApiAuth', 'ApiController@populateApiAuth');

You set the route up for GET requests, but you're trying to access it via a POST request.

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

3 Comments

I quite literally just realized that as I hit submit... Feeling really stupid now, and yes that solved my issue. FACEPALM
@user3771990 It happens. Sometimes, writing it out can allow you to see something that you overlooked before :)
I will accept your answer when it lets me, and let this be an example for future Laravelers that fall into the same dumb mistake! :)

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.