Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

280
Views
Can not post in laravel 5 over jquery Ajax

I'm trying to send a POST request over jquery Ajax in Laravel 5.1 application. I got 405 method not allow, I'm search other questions on this forum but not find solution:

My routes.php:

Route::post('backend/get_subdirectories',  'Backend\FileManagerController@get_subdirectories');

The Controller

public function get_subdirectories(Request $request)
{
    dd($request);
}

And script

var _token = $('meta[name="csrf-token"]').attr('content');
console.log(_token); //It work, I can get my token from meta tag
$.post(
            'http://domain.com/backend/get_subdirectories/',
            { _token: _token},
            function () {
                alert("success");
            })
            .fail(function () {
                alert("error");
            })
            .always(function () {
                alert("finished");
            });

And I got error 405 - Method not allowed

What am I wrong ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

@Chris's comment is correct :)

You simply need to remove the / from the end of your url. Your ajax request should go to http://domain.com/backend/get_subdirectories.

The reason is, because within the public/.htaccess file it will 301 redirect all urls with a trailing slash to the same url without one. The code that does it is here:

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

Now the real issue is, the client will preform a GET request to the URL specified by the 301 redirect.

Wait! Why would it do this???

Well, we can look to RFC7231 for the answer. It says

6.4.2. 301 Moved Permanently

The 301 (Moved Permanently) status code indicates that the target
resource has been assigned a new permanent URI and any future
references to this resource ought to use one of the enclosed URIs.
Clients with link-editing capabilities ought to automatically re-link references to the effective request URI to one or more of the new
references sent by the server, where possible.

The server SHOULD generate a Location header field in the response containing a preferred URI reference for the new permanent URI. The
user agent MAY use the Location field value for automatic
redirection. The server's response payload usually contains a short
hypertext note with a hyperlink to the new URI(s).

  Note: For historical reasons, a user agent MAY change the request
  method from POST to GET for the subsequent request.  If this
  behavior is undesired, the 307 (Temporary Redirect) status code
  can be used instead.

A 301 response is cacheable by default; i.e., unless otherwise
indicated by the method definition or explicit cache controls (see
Section 4.2.2 of [RFC7234]).

Now what's interesting is the note at the bottom that specifies that the user agent MAY change the request method from POST to GET. And it seems most user agents from browsers to frameworks, seem to follow that rule.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!