2 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
什么是因?yàn)槟褂缅e(cuò)誤的 MIME 類型為您的 css 文件提供服務(wù),您應(yīng)該為 css 設(shè)置正確的標(biāo)題。利用:
func serveCss(w http.ResponseWriter, r *http.Request) {
// some code here
w.Header().Add("Content-Type", "text/css")
// some code here
}

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
問題是您的 csshandler 返回文件的內(nèi)容,其中 Content-Type 設(shè)置為“text/plain”。您必須將其設(shè)置為“text/css”才能讓瀏覽器將其解釋為 CSS 文件。您可以在使用類似中間件的函數(shù)返回文件內(nèi)容之前設(shè)置內(nèi)容類型:
func SetHeader(header,value string, handle http.Handler) func(http.ResponseWriter,*http.Request) {
return func(w http.ResponseWriter,req *http.Request) {
w.Header().Set(header,value)
handle.ServeHTTP(w,req)
}
}
r.PathPrefix("/css/").HandlerFunc(SetHeader("Content-Type","text/css",http.StripPrefix("/src/static/css/", cssHandler)))
- 2 回答
- 0 關(guān)注
- 199 瀏覽
添加回答
舉報(bào)