golange 错误如下 Resource interpreted as Script but transferred with MIME type text

2014-11-24 03:12:45 · 作者: · 浏览: 3

这是由于服务器端给你发回的java script http响应的content-type值是text/plain(默认。)而你所期望返回的是兼容java script类型的。

解决方法,可以在服务器端的返回字段里增加:content-type : application/x-java script

具体在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-java script")
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-java script")
t, _ := template.ParseFiles("demo_workers.js")
t.Execute(w, nil)
default:
t, _ := template.ParseFiles("demo_workers.html")
t.Execute(w, nil)
}
}