Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript获取api使用golang mux路由器获取404响应_Javascript_Go_Http Status Code 404_Fetch Api_Mux - Fatal编程技术网

Javascript获取api使用golang mux路由器获取404响应

Javascript获取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"

我试图使用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">
  <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)
}