我正在尝试在 Golang 函数内运行 JavaScript,并使用 fetch 在 Javascript 上下文中通过 API 获取 JSON。
我使用以下代码在Otto中进行了尝试:
import "github.com/robertkrimen/otto"
vm := otto.New()
vm.Run(`
function tryfunc() {
console.log("Started");
fetch('https://www.example.com/APIendpoint');
console.log("Success");
}
tryfunc();
`)
Otto 使用起来非常简单,但看起来 Otto 是一个事件总线并且不管理 fetch。
我现在正在尝试使用以下代码使用v8go :
import v8 "rogchap.com/v8go"
ctx := v8.NewContext()
ctx.RunScript(`fetch("https://www.example.com/APIendpoint"), ""`)
但它需要另一个论点。文档非常不清楚,即使是最简单的 JS 脚本也很难理解如何运行。
有人可以帮忙吗?
ibeautiful
相关分类