custom fetch() 在 office 加载项中没有收到任何响应,

问题描述 投票:0回答:0

我用react开发office add-in,定义了一个从服务器获取数据的函数。 服务器收到请求并做出成功响应。 但请求没有得到回应 我错过了什么或做错了什么吗?

async function sendRequest() {
    const param = "abb";
    var res = await myRequest(param);
    return Word.run(async (context) => {
      const doc = context.document;
      const originalRange = doc.getSelection();
      originalRange.insertText(res.data, Word.InsertLocation.before);
      await context.sync();
    });
  }
function myRequest(payload) {// the same as the example in Microsoft document
    const url = `http://127.0.0.1:8080/complete/keyword?text=${payload}`;
    return new Promise(function (resolve, reject) {
      fetch(url)
        .then(function (response) {
          return response.json();
        })
        .then(function (json) {
          resolve(JSON.stringify(json.names));
        });
    });
  }

我尝试了 XMLHttpRequest,它确实收到了来自服务器的响应。但结果很奇怪: enter image description here

node.js reactjs office-js office-addins
© www.soinside.com 2019 - 2024. All rights reserved.