代码之家  ›  专栏  ›  技术社区  ›  Jimmy

从获取请求获取html

  •  0
  • Jimmy  · 技术社区  · 7 年前

    我正在尝试从api响应获取html。如果执行以下获取请求:

    const fetchHtml = () => {
      return fetch('local:8080/api/getHtml')
        .then((response) => {
          console.log("Response:");
          console.log(response.text());
        })
        .then((data) => {
          console.log("Data:");
          console.log(data);
        });
      };
    };
    

    Response:
    Promise {<pending>}
      __proto__: Promise
      [[PromiseStatus]]: "resolved"
      [[PromiseValue]]: "<html><head><title>Your interest in PROGRAM_NAME</ `Show 108 096 more Copy`"
    Data:
    undefined
    

    如何从fetchapi请求中获取返回的html promise值?谢谢。

    1 回复  |  直到 7 年前
        1
  •  3
  •   samnu pel    7 年前

    您必须返回response.text from。然后

    const fetchHtml = () => {
      return fetch('local:8080/api/getHtml')
        .then((response) => {
          return response.text();
        }).then((text) => {
          console.log(text);
        });
      };
    };