Uncaught SyntaxError: 预期外的标记 <

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

Uncaught SyntaxError: Unexpected token <

问题

我正在使用go运行一个服务器。当我访问localhost:8888/static/ajax.html时,没有出现错误。但是当我只访问localhost:8888时,出现了以下错误:

> "Uncaught SyntaxError: Unexpected
> token <"

默认情况下,"/"会提供ajax.html文件,但是这样做我没有得到预期的结果。另一方面,调用/static/ajax.html时,我得到了预期的结果,没有任何错误。

server.go包含以下内容:

  1. package main
  2. import (
  3. &quot;http&quot;
  4. &quot;flag&quot;
  5. )
  6. //var path = flag.String(&quot;storage&quot;, &quot;/home/chinmay/work/jstree/&quot;, &quot;Set storage directory, use absolute path&quot;)
  7. var root = flag.String(&quot;root&quot;, &quot;/home/chinmay/work/ajax/&quot;, &quot;Set root directory, use absolute path&quot;)
  8. func temp(w http.ResponseWriter, r *http.Request){
  9. http.ServeFile(w,r,*root +&quot;ajax.html&quot;)
  10. }
  11. func main(){
  12. http.HandleFunc(&quot;/&quot;, temp)
  13. http.Handle(&quot;/static/&quot;, http.FileServer(*root, &quot;/static/&quot;))
  14. http.ListenAndServe(&quot;:8888&quot;, nil)
  15. }

ajax.html包含以下内容:

  1. &lt;html&gt;
  2. &lt;head&gt;
  3. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/jquery.js&quot;&gt;&lt;/script&gt;
  4. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/three/Three.js&quot;&gt;&lt;/script&gt;
  5. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/Detector.js&quot;&gt;&lt;/script&gt;
  6. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/RequestAnimationFrame.js&quot;&gt;&lt;/script&gt;
  7. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/Stats.js&quot;&gt;&lt;/script&gt;
  8. &lt;script type=&quot;text/javascript&quot;&gt;
  9. $.ajaxSetup ({
  10. cache:false;
  11. })
  12. $(&quot;#container&quot;).ready(function(){
  13. $(&quot;button&quot;).click(function(){
  14. $.getScript(&quot;model.js&quot;);
  15. });
  16. });
  17. &lt;/script&gt;
  18. &lt;/head&gt;
  19. &lt;body&gt;
  20. &lt;button&gt;Use Ajax to get and then run a JavaScript&lt;/button&gt;
  21. &lt;div id=&quot;container&quot;&gt;
  22. &lt;/div&gt;
  23. &lt;/body&gt;
  24. &lt;/html&gt;

socket.js : http://www.grideads.net/redmine/attachments/download/113/socket.js
model.js : http://www.grideads.net/redmine/attachments/download/112/model.js

英文:

I am running a server on go. When I access the localhost:8888/static/ajax.html,
I get no errors. But when I just access localhost:8888 I get
an error saying:

> "Uncaught SyntaxError: Unexpected
> token <"

By default "/" serves ajax.html file, but doing so I don't get the
expected result. On the other hand on calling /static/ajax.html I am
getting the expected result without any errors.

server.go contains the following :

  1. package main
  2. import (
  3. &quot;http&quot;
  4. &quot;flag&quot;
  5. )
  6. //var path = flag.String(&quot;storage&quot;, &quot;/home/chinmay/work/jstree/&quot;, &quot;Set storage directory, use absolute path&quot;)
  7. var root = flag.String(&quot;root&quot;, &quot;/home/chinmay/work/ajax/&quot;, &quot;Set root directory, use absolute path&quot;)
  8. func temp(w http.ResponseWriter, r *http.Request){
  9. http.ServeFile(w,r,*root +&quot;ajax.html&quot;)
  10. }
  11. func main(){
  12. http.HandleFunc(&quot;/&quot;, temp)
  13. http.Handle(&quot;/static/&quot;, http.FileServer(*root, &quot;/static/&quot;))
  14. http.ListenAndServe(&quot;:8888&quot;, nil)
  15. }

ajax.html contains the following:

  1. &lt;html&gt;
  2. &lt;head&gt;
  3. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/jquery.js&quot;&gt;&lt;/script&gt;
  4. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/three/Three.js&quot;&gt;&lt;/script&gt;
  5. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/Detector.js&quot;&gt;&lt;/script&gt;
  6. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/RequestAnimationFrame.js&quot;&gt;&lt;/script&gt;
  7. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/Stats.js&quot;&gt;&lt;/script&gt;
  8. &lt;script type=&quot;text/javascript&quot;&gt;
  9. $.ajaxSetup ({
  10. cache:false;
  11. })
  12. $(&quot;#container&quot;).ready(function(){
  13. $(&quot;button&quot;).click(function(){
  14. $.getScript(&quot;model.js&quot;);
  15. });
  16. });
  17. &lt;/script&gt;
  18. &lt;/head&gt;
  19. &lt;body&gt;
  20. &lt;button&gt;Use Ajax to get and then run a JavaScript&lt;/button&gt;
  21. &lt;div id=&quot;container&quot;&gt;
  22. &lt;/div&gt;
  23. &lt;/body&gt;
  24. &lt;/html&gt;

socket.js : http://www.grideads.net/redmine/attachments/download/113/socket.js
model.js : http://www.grideads.net/redmine/attachments/download/112/model.js

答案1

得分: 1

我没有解码问题,但可能的问题是你调用脚本的方式

  1. $.getScript("model.js")

http://localhost:8888/ 会调用 http://localhost:8888/model.js

http://localhost:8888/static/ajax.html 会调用 http://localhost:8888/static/model.js

编辑
另外,你的 model.js 在第133行有一个错误

  1. for( j = 0, jl = meshes.length; j &lt; jl; j++ )

正确的 for 循环格式是

  1. for (变量=起始值; 变量<=结束值; 变量=变量+增量)

j < jl; 是多余的,导致出现 "Unexpected token <" 错误消息

英文:

I did not decode the question, but a possible issue is the way you call your script

  1. $.getScript(&quot;model.js&quot;)

http://localhost:8888/ will call http://localhost:8888/model.js

http://localhost:8888/static/ajax.html will call http://localhost:8888/static/model.js

EDIT
also your model.js has an error on line 133

  1. for( j = 0, jl = meshes.length; j &lt; jl; j++ )

the proper for loop format is

  1. for (variable=startvalue;variable&lt;=endvalue;variable=variable+increment)

j < jl; is extra, resulting in the "Unexpected token <" error message

huangapple
  • 本文由 发表于 2011年7月10日 16:01:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/6639955.html
匿名

发表评论

匿名网友

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

确定