Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

slim Invalid setting #8

Closed
sunyinggang opened this issue Sep 26, 2018 · 7 comments
Closed

slim Invalid setting #8

sunyinggang opened this issue Sep 26, 2018 · 7 comments

Comments

@sunyinggang
Copy link

In my slim project,Still reporting an error:No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access.
My setting:
$set = [
'allow-credentials' => false, // set "Access-Control-Allow-Credentials" 👉 string "false" or "true".
'allow-headers' => [''], // ex: Content-Type, Accept, X-Requested-With
'expose-headers' => [],
'origins' => ['
'], // ex: http://localhost
'methods' => ['*'], // ex: GET, POST, PUT, PATCH, DELETE
'max-age' => 0,
];
$app->add(new Medz\Cors\Slim\Cors($set));

@medz
Copy link
Owner

medz commented Sep 27, 2018

origins 添加你需要跨域的域,测试可以添加 *

例如:

$config = [
    'allow-credentials' => false, // set "Access-Control-Allow-Credentials" 👉 string "false" or "true".
    'allow-headers'      => ['*'], // ex: Content-Type, Accept, X-Requested-With
    'expose-headers'     => [],
    'origins'            => ['*'], // 允许跨域只添加 * 即可
    'methods'            => ['*'], // ex: GET, POST, PUT, PATCH, DELETE
    'max-age'            => 0,
];
$app->add(new Medz\Cors\Slim\Cors($config));

@sunyinggang
Copy link
Author

sunyinggang commented Sep 27, 2018

origins 添加你需要跨域的域,测试可以添加 *

例如:

$config = [
    'allow-credentials' => false, // set "Access-Control-Allow-Credentials" 👉 string "false" or "true".
    'allow-headers'      => ['*'], // ex: Content-Type, Accept, X-Requested-With
    'expose-headers'     => [],
    'origins'            => ['*'], // 允许跨域只添加 * 即可
    'methods'            => ['*'], // ex: GET, POST, PUT, PATCH, DELETE
    'max-age'            => 0,
];
$app->add(new Medz\Cors\Slim\Cors($config));

还是遇到了问题,而且就算是我在nginx也设置一下允许跨域,也是会报错:Failed to load http://wx.simplesay.xin/user/index: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access.

@medz
Copy link
Owner

medz commented Sep 27, 2018

@sunyinggang 好的,我再测试一下回复你~

@sunyinggang
Copy link
Author

@sunyinggang好的,我再测试一下回复你〜

有没有测试通过呀

@medz
Copy link
Owner

medz commented Sep 28, 2018

@sunyinggang

Slim 框架支持演示.zip

这是我写给你的 Slim 框架继承 Cors 例子,包含了跨域测试,解压后请阅读 readme.md 文件

@medz medz closed this as completed Sep 28, 2018
@sunyinggang
Copy link
Author

@sunyinggang

Slim框架支持演示.zip

这是我写给你的Slim框架继承Cors例子,包含了跨域测试,解压后请阅读readme.md文件

感谢您的帮助,现在有效了,问题是我的new \Medz\Cors\Slim\Cors($setting),我没有附带第二个参数true,我发现如果去掉这个参数,就会无效,但是我在您的官方文档中并没有看到对这个的解释,是不是以后使用的时候默认都得带上这个参数,或者在官方实例中改为带第二参数的这种形式,会比较好

@medz
Copy link
Owner

medz commented Sep 29, 2018

@sunyinggang
https://github.com/medz/cors/blob/master/docs/using-by-slim.md#cors-middleware-__construct
在这里有讲两个参数,昨天发了一个新版本,第二个参数默认由 false 改为 true 了,其实第二个参数 false 无伤大雅,因为不同开启必须预检后,只需要验证 OPTIONS 请求即可的,第二个参数为 true 表示不管是不是 预检请求都携带,为 false 就是只有预检才携带跨域信息。我测试过如果在你发送的跨域请求前由预检就完全 ok 的~

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants