這是由於服務器端給你發回的javascript http響應的content-type值是text/plain(默認。)而你所期望返回的是兼容javascript類型的。
解決方法,可以在服務器端的返回字段裡增加:content-type : application/x-javascript
具體在golang的解決方案如下:
[cpp]
func router(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
url := r.URL.Path
switch{
case "/demo_workers.js" == url:
w.Header().Add("Content-Type", "application/x-javascript")
t, _ := template.ParseFiles("demo_workers.js")
t.Execute(w, nil)
default:
t, _ := template.ParseFiles("demo_workers.html")
t.Execute(w, nil)
}
}
func router(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
url := r.URL.Path
switch{
case "/demo_workers.js" == url:
w.Header().Add("Content-Type", "application/x-javascript")
t, _ := template.ParseFiles("demo_workers.js")
t.Execute(w, nil)
default:
t, _ := template.ParseFiles("demo_workers.html")
t.Execute(w, nil)
}
}