类型错误:在 ServiceNow 中运行客户端脚本时,fetch 不是函数

发布于 2025-01-14 09:13:39 字数 974 浏览 6 评论 0原文

我正在 ServiceNow 中开发一个应用程序,其中有以下 UI:

service now UI

当按下提交时,我必须发出 POST 请求。到目前为止,我已经得到以下线索来提出请求。

您可以使用本机 Fetch API 进行任意 HTTP 调用,或者如果您要对自己的 ServiceNow 实例进行 API 调用,则可以使用 helpers.snHttp 方法

Fetch API 看起来就像我在这里需要的东西。当我尝试运行以下脚本时,出现错误。
脚本:

function sendRequest() {
    var test = '';
    try{ fetch('https://dummyjson.com/products/1')
        .then(res => res.json())
        .then(json => test = json);
    }catch(e){return e;}
    return test;
}

错误
TypeError: fetch is not a function

我没有找到摆脱此错误的方法。我对 ServiceNow 脚本完全陌生。我有什么遗漏的吗?

更新1
输入图片此处描述

I am working on an application in ServiceNow where I have the following UI:

service now UI

I have to make a POST request when Submit is pressed. Till now I have got the following lead to make a request.

You can use the native Fetch API to make arbitrary HTTP calls, or you can use the helpers.snHttp method if you are going to be making API calls to your own ServiceNow instance

The Fetch API looks like something that I need here. When I try to run the following script, I get an error.
Script:

function sendRequest() {
    var test = '';
    try{ fetch('https://dummyjson.com/products/1')
        .then(res => res.json())
        .then(json => test = json);
    }catch(e){return e;}
    return test;
}

Error
TypeError: fetch is not a function

I am not finding a way to get rid of this error. I am totally new to ServiceNow scripting. Is there anything I am missing out on?

update 1
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朮生 2025-01-21 09:13:39

根据您的屏幕截图,您正在使用 UI Builder,这似乎受到 ServiceNow 的限制。这就是为什么你不能使用 fetch,我认为你必须使用以下内容来发出 HTTP 请求

helpers
.snHttp("/api/now/table/incident", {
  method: "POST",
  body: {
    description: "Sample description",
    close_notes: "Sample close notes",
    order: "-1"
  }
})
.then(({ response }) => {
  // handle POST request response
})
.catch(({ error }) => {
  // handle POST request errors
});

ServiceNow 文档链接 - helpers.snHttp

Based on your screenshot you are working with UI Builder, which seems to be restricted by ServiceNow. That's why you can't use fetch, I think you have to use the following to make HTTP Requests

helpers
.snHttp("/api/now/table/incident", {
  method: "POST",
  body: {
    description: "Sample description",
    close_notes: "Sample close notes",
    order: "-1"
  }
})
.then(({ response }) => {
  // handle POST request response
})
.catch(({ error }) => {
  // handle POST request errors
});

Link to ServiceNow Documentation - helpers.snHttp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文