【发布时间】:2021-10-18 20:19:25
【问题描述】:
我在 Play 中有应用。我的 application.conf 文件是:
include "silhouette.conf"
play.modules.enabled += "modules.SilhouetteModule"
play.filters.enabled += "play.filters.cors.CORSFilter"
play.filters.hosts {
allowed = ["."]
}
play.filters.cors {
pathPrefixes = ["/"]
allowedOrigins = null
allowedHttpMethods = ["GET", "POST", "PUT", "DELETE"]
allowedHttpHeaders = null
}
所以,我们可以看到所有来源都应该去。
我的前端获取请求如下所示(在 react 应用中):
export function signIn(email, password) {
const host = "http://localhost:9000/"
const route = "signIn";
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: email, password: password }),
credentials: 'include',
};
return fetch(host + route, requestOptions)
}
Chrome 显示:
Access to fetch at 'http://localhost:9000/signIn' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
是的,我尝试使用'no-cors',但它仍然不起作用;)非常不清楚,我不知道它是如何工作的以及如何配置它。
【问题讨论】:
-
我不知道play或者scala但是我知道这个,请加
origin: true或者origin: ['http://localhost:3000']
标签: reactjs scala playframework fetch silhouette