我有一个用于商店的 Nginx 服务器。该商店有一个 API,它位于我使用别名的不同文件夹中。这是我example.com
在sites-enabled
Nginx 中的:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.example.com;
root /var/www/store/public;
#certificated go here [edited]
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com;
root /var/www/store/public;
index index.php;
#certificates go here [edited]
location /admin {
index index.php;
}
location /api {
alias /var/www/api/public;
try_files $uri $uri/ @api;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
location @api{
rewrite /api/(.*)$ /api/index.php?/$1 last;
}
#the rest of the store config block [edited]
}
我遇到的问题是,如果我POST
使用 ajax 向“example.com/api”发送请求,它会更改为 aGET
并且所有参数都将丢失。
这是我的Ajax
代码:
var foo = 'foo';
var bar = 'bar';
$.ajax({
type: 'POST',
beforeSend: function(request) {
request.setRequestHeader("Authorization", 'xyz');
},
url: "https://example.com/api",
data: { foo: foo, bar: bar},
processData: false,
success: function(data) {
console.log(data);
}
});
为了测试这一点,我只是转储了$_SERVER['REQUEST_METHOD']
。
如果我example.com/api/index.php
要求请求类型是应该的,POST
但如果我只是调用example.com/api
它就会更改为GET
.
如何保留请求的请求类型和参数?