【发布时间】:2019-08-22 04:36:56
【问题描述】:
我正在尝试在我的 react-redux 应用程序中使用 axios.create 访问 JSON 数据。我已将 thunk 安装为中间件。我收到“请求的资源上不存在‘Access-Control-Allow-Origin’标头”错误。 根据我在网上阅读的内容,我可以通过在 axios.create 调用中添加标头来解决这个问题。 我该怎么做呢?
//api file
import axios from 'axios';
export default axios.create({
baseURL: 'https://www.thehomelike.com/search/'
})
//actions/index.js
import jsonPlaceholder from '../apis/jsonPlaceholder';
export const fetchPosts = () => {
return async dispatch => {
const response = await jsonPlaceholder.get('DE/Berlin?country=DE&east=13.499539418719593&lang=en-GB&lat=52.52000659999999&lng=13.404953999999975&locality=Berlin&mapLimit=24&north=52.5942101138977&south=52.445677542691186&west=13.310368581316283');
dispatch({ type: 'FETCH_POSTS', payload: response })
}
}
这些是根据 chrome 的网络工具的标题。
Response Headers:
access-control-allow-credentials: true
access-control-allow-headers: authorization
access-control-allow-methods: GET,HEAD,PUT,PATCH,POST,DELETE
access-control-allow-origin: https://www.thehomelike.com
date: Mon, 01 Apr 2019 06:23:11 GMT
server: nginx
status: 204
vary: Origin, Access-Control-Request-Headers
x-powered-by: Express
【问题讨论】:
标签: reactjs redux react-redux axios