gpt4 book ai didi

ajax - Slim Framework - jQuery $.ajax 请求 - Access-Control-Allow-Methods 不允许方法 DELETE

转载 作者:行者123 更新时间:2023-12-04 14:59:23 27 4
gpt4 key购买 nike

我正在尝试使用以 Slim Framework 编写的 REST API。

Get & Post 方法可以正常工作。但是 DELETE 请求不起作用。我收到“Access-Control-Allow-Methods 不允许方法 DELETE”

我已经在标题中允许 OPTIONS 和 DELETE 。请参阅下面的代码。

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');

$app->options('/(:name+)', function() use($app) {
$response = $app->response();
$app->response()->status(200);
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, X-authentication, X-client');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
});

此请求失败的原因可能是什么?

最佳答案

当前版本的 Nginx (1.0.x) 似乎不支持 HTTP OPTIONS 请求。每当发送此请求时,它都会返回 405“Method Not Allowed”。我在 nginx 服务器的配置文件中添加了标题,这解决了我的问题。

location / {
alias /usr/share/nginx/webapp/;
try_files $uri $uri/ /index.php;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Allow-Methods' "GET, POST, OPTIONS, DELETE";
add_header 'Access-Control-Max-Age' 1728000;
return 200;
}

}

——

关于ajax - Slim Framework - jQuery $.ajax 请求 - Access-Control-Allow-Methods 不允许方法 DELETE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144847/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com