Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
768 views
in Technique[技术] by (71.8m points)

javascript - POST 405 (Method not allowed) when trying to post AJAX request - Laravel 4

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...