【问题标题】:managing CORS with fetch API GET request使用 fetch API GET 请求管理 CORS
【发布时间】:2018-05-22 00:48:56
【问题描述】:

我在 localhost:8080/enquiry 有一个终点

呈现以下 JSON:

[{"_id":"5a283e4c5a36f4556af34742",
   "firstName":"bob",
   "surname":"hoskins",
   "telephoneNumber":939483948,
   "gender":"male",
   "dayOfBirth":17,
   "monthOfBirth":5,
   "yearOfBirth":1978,"comments":"hello",
   "emailAddress":"jimmsrrs@gmail.com",
   "createdAt":"2017-12-06T19:00:28.401Z"}]

我有一个看起来像这样的动作创建器

export const receiveEnquiries = enquiries => ({
    type: RECEIVE_ENQUIRIES,
    enquiries 

  });
export const fetchEnquiries = () => dispatch => (
    enquiryAPIUtil
        .fetchEnquiries() // this is the fetch call in api.js below
        .then((enquiries) => {dispatch(receiveEnquiries(enquiries))})
    );

我在 api.js 中有一个如下所示的获取请求:

export const fetchEnquiries = () =>
    fetch(`${api}/enquiry`, { headers })
    .then(res => {
        res.json()
        console.log("res",res)
    })
    .then(data => console.log("data",data))

在控制台中而不是记录上面的 JSON,它记录以下内容:

res Response {type: "cors", url: "http://localhost:8080/enquiry", 
redirected: false, status: 200, ok: true, …} 

在 express server.js 我有

 const cors = require('cors')
 app.use(cors())

(以及呈现 JSON 的代码)

我想知道我是否更有可能在服务器端的客户端做错了什么?

【问题讨论】:

  • 由于您要返回敏感数据,因此不应为所有来源启用 CORS。

标签: javascript express cors react-redux fetch


【解决方案1】:

res.json() 返回从 JSON 解析的对象的承诺。

你忽略了它返回的对象,所以你没有得到任何 JSON。

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 2018-06-07
    • 2017-03-23
    • 2016-02-16
    • 1970-01-01
    • 2019-10-16
    • 2020-07-19
    • 2020-11-27
    • 1970-01-01
    相关资源
    最近更新 更多