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:

我正在尝试通过Laravel 5.1应用程序中的jquery Ajax发送POST请求。我有405方法不允许,我在这个论坛上搜索其他问题,但找不到解决方案:

My routes.php:

我的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

我得到错误405 - 方法不允许

What am I wrong ?

我错了什么?

1 个解决方案

#1


14

@Chris's comment is correct :)

@Chris的评论是正确的:)

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

您只需要从网址的末尾删除/。您的ajax请求应该转到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:

原因是,因为在public / .htaccess文件中,它将使用尾部斜杠将所有URL重定向到没有一个的同一URL。这样做的代码在这里:

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

现在真正的问题是,客户端将对301重定向指定的URL执行GET请求。

Wait! Why would it do this???

等待!它为什么要这样做?

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

好吧,我们可以查看RFC7231的答案。它说

6.4.2. 301 Moved Permanently

6.4.2。 301永久移动

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.

301(永久移动)状态代码表示已为目标资源分配了新的永久URI,并且此资源的任何将来引用都应使用其中一个附带的URI。具有链接编辑功能的客户端应尽可能自动将对有效请求URI的引用重新链接到服务器发送的一个或多个新引用。

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

服务器应该在响应中生成一个Location头字段,其中包含新永久URI的首选URI引用。用户代理可以使用Location字段值进行自动重定向。服务器的响应有效负载通常包含一个短超文本注释,其中包含指向新URI的超链接。

  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]).

默认情况下,301响应可缓存;即,除非方法定义或显式高速缓存控制另有说明(参见[RFC7234]的第4.2.2节)。

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.

现在有趣的是底部的注释指定用户代理可以将请求方法从POST更改为GET。似乎从浏览器到框架的大多数用户代理似乎遵循该规则。

更多相关文章

  1. Jquery注册服务器控件事件与判断控件的值
  2. 与symfony一起使用ajax时出现500内部服务器错误
  3. 关于js 方法,实现ajax请求时,return false/true无效的问题
  4. 在jQuery Mobile中做$(document).ready的正确方法是什么?
  5. ie8下jQuery动画没有ie7流畅,是浏览器性能问题吗?有没有什么解决方
  6. 有什么方法可以让jQuery循环在到达终点或开始时不循环?
  7. js限制文本框只能输入数字方法
  8. 模拟JQuery的Ready方法实现并出现的问题
  9. html 页面内锚点定位及跳转方法总结

随机推荐

  1. android面试与总结
  2. Android - 设置带滚动条的TextView
  3. Android EditText输入的一些限制
  4. Android(安卓)ListView拖动时背景颜色会
  5. Android 源码 修改系统默认横屏
  6. A3```在android native c里打log
  7. Android studio gradle 生成字段属性值
  8. Android 制作 升级包
  9. Android: persist instance state across
  10. android 算定义布局xml