第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 GO Gorilla mux 服務(wù)器應(yīng)用 CSS 文件時(shí)出現(xiàn) MIME 類型錯(cuò)誤

使用 GO Gorilla mux 服務(wù)器應(yīng)用 CSS 文件時(shí)出現(xiàn) MIME 類型錯(cuò)誤

Go
烙印99 2022-06-06 16:46:21
我在使用 Gorilla Mux 在 GO 網(wǎng)絡(luò)服務(wù)器中包含 css 文件時(shí)遇到問題。我在 Google Chrome 控制臺(tái)中收到以下錯(cuò)誤:forum:1 Refused to apply style from 'http://localhost:8080/css/forum.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.我知道很多人在使用 FileServer 時(shí)會(huì)因?yàn)樘幚?quot;/"錯(cuò)誤而失敗,但這對(duì)我也不起作用。我的文件結(jié)構(gòu)如下: 文件結(jié)構(gòu) 當(dāng)我運(yùn)行服務(wù)器時(shí),我在 cmd: 中執(zhí)行g(shù)o run src/main.go。我也嘗試在src文件夾中運(yùn)行它。但這也行不通。在 HTML 文件中,我添加了 css 文件<link rel="stylesheet" type="text/css" href="/css/forum.css" />我的 GO 代碼如下。我嘗試以兩種方式處理 FileServer,其中一種在另一種上方被注釋掉。兩者都行不通。除了 FileServer,其他一切都在工作。package mainimport (    "fmt"    "net/http"    "html/template"    "github.com/gorilla/mux")var templates *template.Templatefunc main() {    r := mux.NewRouter()    templates = template.Must(template.ParseGlob("src/templates/*.html"))    cssHandler := http.FileServer(http.Dir("./static/css"))    r.HandleFunc("/home", homeGetHandler).Methods("GET")    r.HandleFunc("/home", homePostHandler).Methods("POST")    r.HandleFunc("/forum", forumGetHandler).Methods("GET")    r.HandleFunc("/forum", forumPostHandler).Methods("POST")    http.Handle("/forum", r)    http.Handle("/home", r)    // http.Handle("/css/", http.StripPrefix("/src/static/css/", cssHandler))    r.PathPrefix("/css/").Handler(http.StripPrefix("/src/static/css/", cssHandler))    http.ListenAndServe(":8080", nil)}func homeGetHandler(w http.ResponseWriter, r *http.Request) {    templates.ExecuteTemplate(w, "home.html", nil)}func homePostHandler(w http.ResponseWriter, r *http.Request) {    r.ParseForm()    comment := r.PostForm.Get("comment")    fmt.Println(comment)    http.Redirect(w, r,"/home", 302)}func forumGetHandler(w http.ResponseWriter, r *http.Request) {    templates.ExecuteTemplate(w, "forum.html", nil)}func forumPostHandler(w http.ResponseWriter, r *http.Request) {    r.ParseForm()    comment := r.PostForm.Get("post")    fmt.Println(comment)    http.Redirect(w, r,"/forum", 302)}[解決方案] 我找到了答案:http.Handle("/forum", r)http.Handle("/home", r)應(yīng)該只是:http.Handle("/",r)
查看完整描述

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

}


查看完整回答
反對(duì) 回復(fù) 2022-06-06
?
至尊寶的傳說

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)))


查看完整回答
反對(duì) 回復(fù) 2022-06-06
  • 2 回答
  • 0 關(guān)注
  • 199 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)