我正在用 Go 構建一個 Web 應用程序。在我嘗試嘗試將我的 index.html 文件鏈接到名為 index.js 的 javascript 文件之前,我在這個項目中沒有遇到任何問題:<script type="text/javascript" src="javascript/index.js"></script>我在 Firefox 中得到的確切錯誤是:由于 MIME 類型(“text/plain”)不匹配(X-Content-Type-Options: nosniff),來自“https://10.78.80.22:8000/javascript/index.js”的資源被阻止我一直在廣泛地進行故障排除。我發現與此錯誤有關的所有解決方案均未成功。只有一篇文章是針對特定的,但解決方案沒有幫助。我已經確保 js 文件的路徑是正確的。我正在使用 gorilla mux 來處理路由。這是一個代碼示例:r := mux.NewRouter()r.HandleFunc("/", middleware.AuthRequired(indexGetHandler)).Methods("GET")r.HandleFunc("/", middleware.AuthRequired(indexPostHandler)).Methods("POST")r.HandleFunc("/login", loginGetHandler).Methods("GET")r.HandleFunc("/login", loginPostHandler).Methods("POST")r.HandleFunc("/logout", logoutHandler).Methods("GET")r.HandleFunc("/register", registerGetHandler).Methods("GET")r.HandleFunc("/register", registerPostHandler).Methods("POST")r.HandleFunc("/clumps", middleware.AuthRequired(clumpsGetHandler)).Methods("GET")r.HandleFunc("/clumps", middleware.AuthRequired(clumpsPostHandler)).Methods("POST") log.Fatal(http.ListenAndServeTLS(":8000", "cert/cert.pem", "cert/key.pem", r))這是我的項目的相關結構:main.go>routes routes.go>templates index.html >javascript index.js請注意,上面的“代碼示例”位于 routes.go這是我的 main.go 文件供您參考:package mainimport ( "log" "net/http" "./routes" "./templates")func main() { templates.LoadTemplates("templates/*.html") r := routes.NewRouter() log.Fatal(http.ListenAndServeTLS(":8000", "cert/cert.pem", "cert/key.pem", r))}編輯:這是索引獲取處理程序(位于路由文件夾中)。如果后處理程序會有所幫助,請告訴我,但它似乎不相關:func indexGetHandler(w http.ResponseWriter, r *http.Request) { templates.Execute(w, "index.html", nil)}這是 index.html:<!DOCTYPE html><html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Compositum</title></head><body> {{ if . }} <div class="error">{{ . }}</div> {{ end }} <h1>Fill it out:</h1></body></html>
來自 Firefox 的錯誤:MIME 類型(“text/plain”)不匹配
慕工程0101907
2022-06-13 16:48:22