类型错误:在 ServiceNow 中运行客户端脚本时,fetch 不是函数
我正在 ServiceNow 中开发一个应用程序,其中有以下 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 脚本完全陌生。我有什么遗漏的吗?
I am working on an application in ServiceNow where I have the following 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的屏幕截图,您正在使用 UI Builder,这似乎受到 ServiceNow 的限制。这就是为什么你不能使用 fetch,我认为你必须使用以下内容来发出 HTTP 请求
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
Link to ServiceNow Documentation - helpers.snHttp