首页 > 解决方案 > 在 React 应用程序中获取 - 如何处理不透明的响应

问题描述

我对 React 和 JS 很陌生,试图从我的 React 组件中的服务获取数据,但没有任何内容:

  fetch("http://localhost:9000/someEndPoint", 
    {
      method: "GET",
      mode: "no-cors",
      credentials: "same-origin",
      cache: "no-cache",
      headers: {
        "Accept" : "application/json; charset=utf-8",
        "Content-Type" : "application/json; charset=utf-8"
      }
    })
    .then((response) => {
        if (!response.ok) {
            throw Error(response.statusText);
        }

        return response.json();
    })
    .then((b) => {

      console.log(b);
    })
    .catch((error) => {
        console.log(error);
    });

更新:由于 no-cors 模式,我得到一个不透明的响应,知道如何处理这个问题吗?

标签: javascriptnode.jsreactjsfetch-api

解决方案


推荐阅读