GO – HTML文件无法加载外部JS文件

huangapple go评论96阅读模式
英文:

GO - html file can't load external js file

问题

我有一个使用HTML编写的GO项目。在HTML文件中,我已经编写了JavaScript,并且可以成功安装GO并加载UI。

问题是当我将这些JavaScript代码移动到一个外部文件中,并包含它时:。我只能构建GO,但在加载UI时,它会出现404 Not Found错误,即"myscripts.js"文件。奇怪的是错误的头部信息:
远程地址:127.0.0.1:8080
请求URL:http://localhost:8080/subscribe/myscripts.js
请求方法:GET
状态码:404 Not Found

请帮忙,我还没有找到任何关于这个问题的答案。

英文:

I have a GO project which has UI written by html. In HTML file, I have written javascript, and I can install GO and load the UI successfully.

The problem is when I move those javascript code to an external file, and include it: <script src="myscripts.js" type="text/javascript"></script>. I only can build GO, but when load the UI, it has error 404 Not Found which is "myscripts.js" file. The weird thing is on its header of error:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/subscribe/myscripts.js
Request Method:GET
Status Code:404 Not Found

Please help, I can't find any answer yet for this

答案1

得分: 3

如果你只为你的端点定义了处理程序,那是正常的。

你应该在你的路由器中添加 http.FileServer 处理程序,指向存储你项目文件的目录。

此外,考虑使用 &lt;base&gt; 来定义相对链接的根目录,这将防止路径嵌套等错误。

英文:

That's normal if you only define handler for your endpoints.

You should add to your router the http.FileServer handler pointing to the directory that store your project files.

Additionally, think about the &lt;base&gt; that to define the root of your relative links, which will prevent errors such as path nesting.

huangapple
  • 本文由 发表于 2015年3月11日 17:49:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/28983221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定