【发布时间】:2016-06-20 14:44:48
【问题描述】:
我编写了一个使用 webpack-dev-server 在本地运行的 redux 应用程序。 (端口 8080)。我正在尝试连接到在端口 9000 本地运行的 Web 服务。
我连接网络服务的代码如下
return fetch(`http://localhost:9000/movies/${dimensionName.toLowerCase()}list`)
.then(response => response.json())
.then(json =>
dispatch(receivedDimensionAttributesSuccess(dimensionName, json))
)
.catch(error =>
dispatch(receivedDimensionAttributesError(dimensionName, error))
);
这会收到错误
Fetch API cannot load http://localhost:9000/movies/yearlist. No 'Access-
Control-Allow-Origin' header is present on the requested resource. Origin
'http://localhost:8080' is therefore not allowed access. If an opaque response
serves your needs, set the request's mode to 'no-cors' to fetch the resource
with CORS disabled.
我搜索了这个问题并找到了这个帖子
Access Control Allow Origin header not present with fetch api call
但我不喜欢涉及完全切换到不同库/中间件的解决方案。
如何解决同构提取库的问题。
【问题讨论】:
-
您的网络服务是否允许 CORS?
-
啊......我不知道需要在 web 服务端配置 CORS。我快速搜索并找到了这个playframework.com/documentation/2.4.x/CorsFilter。实施后,问题得到解决。