UrlFetchApp 无法访问本地主机资源

Rx2*_*000 4 localhost urlfetch google-apps-script

这工作正常:

var resp = UrlFetchApp.fetch("someremotehost/SomeFile.csv");
resp.getResponseCode();  //returns 200
resp.getContentText();   //returns the data
Run Code Online (Sandbox Code Playgroud)

但是,在我的本地计算机上,我正在运行 xampp,SomeFile.csv 位于 htdocs/dev 中,但我无法让它在本地主机上工作:

var resp = UrlFetchApp.fetch("localhost/dev/SomeFile.csv");
resp.getResponseCode(); //returns 0.0
resp.getContentText()   //returns nothing!
Run Code Online (Sandbox Code Playgroud)

我检查了 chrome-extension postman 并且http://localhost/dev/SomeFile.csv工作正常,那么为什么不起作用呢UrlFetchApp.fetch("http://localhost/dev/SomeFile.csv")

Zig*_*del 6

这不起作用,因为应用程序脚本执行服务器端代码(在谷歌服务器中)。唯一的方法是创建一个 htmlService 应用程序并从前端使用 ajax。

  • 如果您将本地主机连接到互联网(例如,使用 DMZ 或路由器上的端口映射),那么您可以使用其*互联网地址*和端口从 Apps 脚本访问它 - 但“本地主机”永远不会解析自动到您的机器。 (3认同)