英文:
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 (
"http"
"flag"
)
//var path = flag.String("storage", "/home/chinmay/work/jstree/", "Set storage directory, use absolute path")
var root = flag.String("root", "/home/chinmay/work/ajax/", "Set root directory, use absolute path")
func temp(w http.ResponseWriter, r *http.Request){
http.ServeFile(w,r,*root +"ajax.html")
}
func main(){
http.HandleFunc("/", temp)
http.Handle("/static/", http.FileServer(*root, "/static/"))
http.ListenAndServe(":8888", nil)
}
ajax.html包含以下内容:
<html>
<head>
<script type="text/javascript" src="/static/jquery.js"></script>
<script type="text/javascript" src="/static/three/Three.js"></script>
<script type="text/javascript" src="/static/js/Detector.js"></script>
<script type="text/javascript" src="/static/js/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="/static/js/Stats.js"></script>
<script type="text/javascript">
$.ajaxSetup ({
cache:false;
})
$("#container").ready(function(){
$("button").click(function(){
$.getScript("model.js");
});
});
</script>
</head>
<body>
<button>Use Ajax to get and then run a JavaScript</button>
<div id="container">
</div>
</body>
</html>
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 (
"http"
"flag"
)
//var path = flag.String("storage", "/home/chinmay/work/jstree/", "Set storage directory, use absolute path")
var root = flag.String("root", "/home/chinmay/work/ajax/", "Set root directory, use absolute path")
func temp(w http.ResponseWriter, r *http.Request){
http.ServeFile(w,r,*root +"ajax.html")
}
func main(){
http.HandleFunc("/", temp)
http.Handle("/static/", http.FileServer(*root, "/static/"))
http.ListenAndServe(":8888", nil)
}
ajax.html contains the following:
<html>
<head>
<script type="text/javascript" src="/static/jquery.js"></script>
<script type="text/javascript" src="/static/three/Three.js"></script>
<script type="text/javascript" src="/static/js/Detector.js"></script>
<script type="text/javascript" src="/static/js/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="/static/js/Stats.js"></script>
<script type="text/javascript">
$.ajaxSetup ({
cache:false;
})
$("#container").ready(function(){
$("button").click(function(){
$.getScript("model.js");
});
});
</script>
</head>
<body>
<button>Use Ajax to get and then run a JavaScript</button>
<div id="container">
</div>
</body>
</html>
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 < 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("model.js")
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 < jl; j++ )
the proper for loop format is
for (variable=startvalue;variable<=endvalue;variable=variable+increment)
j < jl; is extra, resulting in the "Unexpected token <" error message
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论