Javascript获取api使用golang mux路由器获取404响应
我试图使用Fetch API和Gorilla mux Router.Handle函数将JSON数据发布到本地服务器,但它总是返回404状态码,即使我正确输入了URL。通过输入localhost:3000,我成功地在GO local server中运行并呈现了我的HTML、CSS和JS文件 这是我的密码 index.htmlJavascript获取api使用golang mux路由器获取404响应,javascript,go,http-status-code-404,fetch-api,mux,Javascript,Go,Http Status Code 404,Fetch Api,Mux,我试图使用Fetch API和Gorilla mux Router.Handle函数将JSON数据发布到本地服务器,但它总是返回404状态码,即使我正确输入了URL。通过输入localhost:3000,我成功地在GO local server中运行并呈现了我的HTML、CSS和JS文件 这是我的密码 index.html <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1 class="heading">Testing 123</h1>
<button onclick="Go()">HELLO WORLD</button>
<script src="script.js"></script>
</body>
</html>
梅因,加油
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/GeertJohan/go.rice"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
type People struct {
Name string `json:"name"`
Age int64 `json:"age"`
IsAlive bool `json:"isAlive"`
}
func main() {
//Init Router
router := mux.NewRouter()
router.PathPrefix("/").Handler(http.FileServer(rice.MustFindBox("webTest").HTTPBox()))
router.HandleFunc("/testing", testingFeature).Methods("POST")
corsOpts := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "HEAD", "OPTIONS", "PUT"},
AllowCredentials: true,
AllowedHeaders: []string{"Content-Type", "Access-Control-Allow-Origin"},
OptionsPassthrough: true,
})
handler := corsOpts.Handler(router)
http.ListenAndServe(":3000", handler)
}
func testingFeature(w http.ResponseWriter, r *http.Request) {
var people People
getInfo := json.NewDecoder(r.Body).Decode(&people)
fmt.Println("getInfo", getInfo)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(getInfo)
}
是因为PathPrefix在
/
上创建了一条新路由。你可以使用router.Handle(“/”),http.FileServer(rice.MustFindBox(“webTest”).HTTPBox())
或router.PathPrefix(“/files”)
谢谢伙计,我可以通过邮递员得到200的状态码。但是这里有一个问题,在我切换到router.Handle(“/”,http.FileServer(rice.MustFindBox(“webTest”).HTTPBox())
之后,我的外部链接文件(如style.css和script.js)不再起作用。它声明其MIME类型(“text/plain”)不是受支持的样式表MIME类型,并且启用了严格的MIME检查。你知道为什么吗?非常感谢您,因为PathPrefix在/
创建了一条新路由,您可以使用路由器.Handle(“/”,http.FileServer(rice.MustFindBox(“webTest”).HTTPBox())
或路由器.PathPrefix(“/files”)
谢谢,伙计,我可以通过邮递员获得200的状态码。但是这里有一个问题,在我切换到router.Handle(“/”,http.FileServer(rice.MustFindBox(“webTest”).HTTPBox())
之后,我的外部链接文件(如style.css和script.js)不再起作用。它声明其MIME类型(“text/plain”)不是受支持的样式表MIME类型,并且启用了严格的MIME检查。你知道为什么吗?多谢各位
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/GeertJohan/go.rice"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
type People struct {
Name string `json:"name"`
Age int64 `json:"age"`
IsAlive bool `json:"isAlive"`
}
func main() {
//Init Router
router := mux.NewRouter()
router.PathPrefix("/").Handler(http.FileServer(rice.MustFindBox("webTest").HTTPBox()))
router.HandleFunc("/testing", testingFeature).Methods("POST")
corsOpts := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "HEAD", "OPTIONS", "PUT"},
AllowCredentials: true,
AllowedHeaders: []string{"Content-Type", "Access-Control-Allow-Origin"},
OptionsPassthrough: true,
})
handler := corsOpts.Handler(router)
http.ListenAndServe(":3000", handler)
}
func testingFeature(w http.ResponseWriter, r *http.Request) {
var people People
getInfo := json.NewDecoder(r.Body).Decode(&people)
fmt.Println("getInfo", getInfo)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(getInfo)
}