问题描述
我正在尝试通过Laravel 5.1应用程序中的jquery Ajax发送 POST
请求。我有405方法不允许,我在这个论坛上搜索其他问题但找不到解决方案:
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:
我的 routes.php
:
Route::post('backend/get_subdirectories', 'Backend\FileManagerController@get_subdirectories');
控制器
public function get_subdirectories(Request $request)
{
dd($request);
}
和脚本
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");
});
我收到错误405 - 方法不允许
And I got error 405 - Method not allowed
我错了什么?
推荐答案
@ Chris的评论是正确的:)
@Chris's comment is correct :)
您只需从网址末尾删除 /
即可。你的ajax请求应该转到 http://domain.com/backend/get_subdirectories
。
You simply need to remove the /
from the end of your url. Your ajax request should go to http://domain.com/backend/get_subdirectories
.
原因是,因为在 public / .htaccess
文件中,它将301将带有斜杠的所有网址重定向到没有一个的同一网址。执行此操作的代码如下:
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]
现在真正的问题是,客户端会对301重定向指定的URL执行GET请求。
Now the real issue is, the client will preform a GET request to the URL specified by the 301 redirect.
等等!为什么会这样做?
好吧,我们可以看看的答案。它说
Well, we can look to RFC7231 for the answer. It says
301(永久移动)状态代码表示目标
资源已分配了新的永久URI以及任何未来
对此资源的引用应该使用其中一个附带的URI。
具有链接编辑功能的客户端应该自动将
对有效请求URI的引用重新链接到一个或者在可能的情况下,服务器发送的更多新的
引用。
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.
服务器应该在响应
中生成Location头字段包含新永久URI的首选URI引用。
用户代理可以使用Location字段值进行自动
重定向。服务器的响应有效负载通常包含一个短的
超文本注释,其中包含指向新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).
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.
默认情况下,301响应可缓存;即,除非另有说明 - 方法定义或显式缓存控制指示的
(参见[
] [RFC7234]的第4.2.2节)。
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]).
现在有趣的是底部的注释指定用户代理可以将请求方法从POST更改为GET。似乎从浏览器到框架的大多数用户代理似乎遵循该规则。
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.
这篇关于无法通过jquery Ajax在laravel 5中发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!