Uncaught SyntaxError: 预期外的标记 <

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

Uncaught SyntaxError: Unexpected token <

问题

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

> "Uncaught SyntaxError: Unexpected
> token <"

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

server.go包含以下内容:

package main 
import ( 
        &quot;http&quot; 
        &quot;flag&quot; 
) 
//var path = flag.String(&quot;storage&quot;, &quot;/home/chinmay/work/jstree/&quot;, &quot;Set storage directory, use absolute path&quot;) 
var root = flag.String(&quot;root&quot;, &quot;/home/chinmay/work/ajax/&quot;, &quot;Set root directory, use absolute path&quot;) 
func temp(w http.ResponseWriter, r *http.Request){ 
        http.ServeFile(w,r,*root +&quot;ajax.html&quot;) 
} 

func main(){ 
        http.HandleFunc(&quot;/&quot;, temp) 
        http.Handle(&quot;/static/&quot;, http.FileServer(*root, &quot;/static/&quot;)) 
        http.ListenAndServe(&quot;:8888&quot;, nil) 
} 

ajax.html包含以下内容:

&lt;html&gt; 
    &lt;head&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/jquery.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/three/Three.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/Detector.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/RequestAnimationFrame.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/Stats.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot;&gt; 
            $.ajaxSetup ({ 
                cache:false; 
            }) 
            
            $(&quot;#container&quot;).ready(function(){ 
                $(&quot;button&quot;).click(function(){ 
                    $.getScript(&quot;model.js&quot;); 
                }); 
            }); 
        &lt;/script&gt; 
    &lt;/head&gt; 
    &lt;body&gt; 
        &lt;button&gt;Use Ajax to get and then run a JavaScript&lt;/button&gt; 
        &lt;div id=&quot;container&quot;&gt; 
        &lt;/div&gt; 
    &lt;/body&gt; 
&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 :

package main 
import ( 
        &quot;http&quot; 
        &quot;flag&quot; 
) 
//var path = flag.String(&quot;storage&quot;, &quot;/home/chinmay/work/jstree/&quot;, &quot;Set storage directory, use absolute path&quot;) 
var root = flag.String(&quot;root&quot;, &quot;/home/chinmay/work/ajax/&quot;, &quot;Set root directory, use absolute path&quot;) 
func temp(w http.ResponseWriter, r *http.Request){ 
        http.ServeFile(w,r,*root +&quot;ajax.html&quot;) 
} 

func main(){ 
        http.HandleFunc(&quot;/&quot;, temp) 
        http.Handle(&quot;/static/&quot;, http.FileServer(*root, &quot;/static/&quot;)) 
        http.ListenAndServe(&quot;:8888&quot;, nil) 
} 

ajax.html contains the following:

&lt;html&gt; 
    &lt;head&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/jquery.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/three/Three.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/Detector.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/RequestAnimationFrame.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot; src=&quot;/static/js/Stats.js&quot;&gt;&lt;/script&gt; 
        &lt;script type=&quot;text/javascript&quot;&gt; 
            $.ajaxSetup ({ 
                cache:false; 
            }) 
            
            $(&quot;#container&quot;).ready(function(){ 
                $(&quot;button&quot;).click(function(){ 
                    $.getScript(&quot;model.js&quot;); 
                }); 
            }); 
        &lt;/script&gt; 
    &lt;/head&gt; 
    &lt;body&gt; 
        &lt;button&gt;Use Ajax to get and then run a JavaScript&lt;/button&gt; 
        &lt;div id=&quot;container&quot;&gt; 
        &lt;/div&gt; 
    &lt;/body&gt; 
&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

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

$.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行有一个错误

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

正确的 for 循环格式是

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

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

英文:

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

$.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

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

the proper for loop format is

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:

确定