You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Outlook Script Lab中无法执行fetch/HTTP请求求助

问题:Outlook插件Script Lab中Fetch请求报错“Failed to fetch”

我在Outlook插件内通过Script Lab执行一个简单的GET请求,该请求在Postman中可正常运行,但在Script Lab里仅返回TypeError: Failed to fetch错误。

Postman生成的可用请求代码

Fetch

var myHeaders = new Headers();
myHeaders.append("Device", "ffbc262f692a075funknown");

var requestOptions = {
  method: 'GET',
  headers: myHeaders,
  redirect: 'follow'
};

fetch("http://******/admin/App?action=getEntryLogs", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

JQuery

var settings = {
  "url": "http://******/admin/App?action=getEntryLogs",
  "method": "GET",
  "timeout": 0,
  "headers": {
    "Device": "ffbc262f692a075funknown"
  },
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

XHR

// WARNING: For GET requests, body is set to null by browsers.

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function() {
  if(this.readyState === 4) {
    console.log(this.responseText);
  }
});

xhr.open("GET", "http://******/admin/App?action=getEntryLogs");
xhr.setRequestHeader("Device", "ffbc262f692a075funknown");

xhr.send();

我在Script Lab中使用的代码

function GetAccesses(event) {
  const responseType = "json";
  var myHeaders = new Headers();
  myHeaders.append("Device", "ffbc262f692a075funknown");

  var requestOptions: RequestInit = {
    method: "GET",
    headers: myHeaders,
    redirect: "manual"
  };
  fetch("http://******/admin/App?action=getEntryLogs", requestOptions)
    .then((response) => response.text())
    .then((result) => console.log(result))
    .catch((error) => console.log("error",error));
}

错误信息

TypeError {}
message: "Failed to fetch"
stack: "TypeError: Failed to fetch at HTMLButtonElement.GetAccesses (script-lab-runner.azureedge.net/:17:5) at HTMLButtonElement.dispatch (unpkg.com/jquery@3.1.1:5201:27) at elemData.handle (unpkg.com/jquery@3.1.1:5009:28)"
proto: TypeError constructor: function TypeError() message: "" name: "TypeError" proto: Error constructor: function Error() message: "" name: "Error" toString: function toString() proto: Object

Postman正常返回的数据

{
    "data": [
        {
            "result": 10,
            "idAccessServer": 45082,
            "id": 19,
            "idContractServer": 0,
            "idPlant": 3,
            "type": 0,
            "align": 1,
            "idPersonServer": 3523,
            "idServer": 19
        },
        {
            "result": 10,
            "idAccessServer": 45083,
            "id": 20,
            "idContractServer": 0,
            "idPlant": 3,
            "type": 1,
            "align": 1,
            "idPersonServer": 3377,
            "idServer": 20
        }
    ],
    "success": "true"
}

解决思路
  • 跨域资源共享(CORS)限制:Outlook插件运行在浏览器沙箱中,受同源策略约束。Postman不受浏览器CORS限制,所以能正常请求,但Script Lab作为网页环境,目标服务器如果未配置允许script-lab-runner.azureedge.net的跨域请求,就会触发该错误。
  • 请求协议差异:若目标接口是http协议,而Script Lab运行在https环境(Outlook插件默认HTTPS),浏览器会阻止非安全的HTTP请求,这也是常见诱因。
  • redirect参数设置:你代码里用了redirect: "manual",而Postman是redirect: "follow",手动重定向可能导致请求无法正常完成,尤其当目标接口存在跳转时。

排查与修复步骤

  1. 检查CORS配置:联系后端开发,确认目标服务器是否添加Access-Control-Allow-Origin响应头,允许script-lab-runner.azureedge.net或*(仅测试环境可用,生产不建议)访问。
  2. 统一协议:将目标接口改为HTTPS协议,或本地测试时临时允许浏览器加载不安全内容(仅测试用,生产必须HTTPS)。
  3. 调整redirect参数:把requestOptions里的redirect改回"follow",与Postman保持一致,避免重定向处理异常。
  4. 添加认证凭证(若需要):参考Postman的XHR代码,在Fetch请求中添加credentials: "include",如果目标接口需要携带Cookie或认证信息:
var requestOptions: RequestInit = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow",
  credentials: "include"
};
  1. 捕获更详细错误:修改catch块,打印完整错误详情:
.catch((error) => {
  console.log("error", error);
  if (error.name === 'TypeError' && error.message === 'Failed to fetch') {
    console.log("可能是CORS或协议问题");
  }
});

内容的提问来源于stack exchange,提问作者Kitsu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 22:15:55