讨论数量:
@liyu001989 postman 也是返回405,不过我的 php 代码不是允许 options 请求了吗,为什么响应头中会没有允许呢? header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
我是想验证下,我刚才改了下 nginx 的配置加上下面就好了
if ($request_method = OPTIONS ) {
add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Methods "POST, GET, PUT, OPTIONS, DELETE";
add_header Access-Control-Max-Age "3600";
add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization, FOO";
add_header Content-Length 0;
add_header Content-Type text/plain;
return 200;
}
为什么直接改 PHP代码就不行呢,我的代码写错了?如果是,那正确应该怎么写呢?
@XiaohuiLam 我这只是个测试,没用框架。
@Mjlove OPTIONS请求应该直接返回NO CONTENT的,
可能
- nginx 自动弄的,假设你AJAX的 endpoint 是个静态资源的话。我以前踩过 静态资源被 POST 会报405的坑,不清楚 OPTIONS 是否一样。
- 走到了你的 PHP 。
你可以看你的 RESPONSE 头, 有没有 x-powered-by: php *,如果有就是 PHP (你的代码)返的,否则就是NGINX返的
@XiaohuiLam 我刚试了下
- 注释掉 nginx 配置时 options 请求 405 没有 x-powered-by: php * 。
- 加上 nginx 配置时 options 请求成功没有 x-powered-by: php *;get 请求成功有 x-powered-by: php **。
或许 nginx 自动挡下了 options 请求吧。
But
同样的 nginx 配置(注释掉跨域配置),如果使用 laravel 框架加上本教程的扩展包就能访问成功。也不知到问题究竟出在哪里
推荐文章: