为了账号安全,请及时绑定邮箱和手机立即绑定

不允许获取错误方法和内容类型:文本/纯文本

不允许获取错误方法和内容类型:文本/纯文本

Go
眼眸繁星 2022-09-26 15:22:26
我的所有路线都完美运行,除了.每当我使用该方法添加数据时,它都会给我消息。当我检查方法标头内容类型时,它是应用程序/ json,但是当我检查方法标头内容类型时,它是文本/纯;charset = utf-8。所以我认为内容类型一定存在问题。我不明白如何解决这个问题。我附上了屏幕截图以供参考。截图: 路线:CreateGoalPostmethod not allowedGetPostfunc Setup(app *fiber.App) {    app.Get("/goals", controllers.GetGoals)    app.Get("/goals/:id", controllers.GetGoal)    app.Post("/goals/add", controllers.CreateGoal)    app.Put("/goals/:id", controllers.UpdateGoal)    app.Delete("/goals/:id", controllers.DeleteGoal)}控制器:import (    "strconv"    "github.com/gofiber/fiber/v2")type Goal struct {  Id        int    `json:"id"`  Title     string `json:"title"`  Status        bool   `json:"status"`}var goals = []*Goal{    {        Id:        1,        Title:     "Read about Promises",        Status:         true,    },    {        Id:        2,        Title:     "Read about Closures",        Status:         false,    },}func GetGoals(c *fiber.Ctx) error {    return c.Status(fiber.StatusOK).JSON(    // "success":  true,    // "data": fiber.Map{            // "goals": goals,        // },        goals,  )}func CreateGoal(c *fiber.Ctx) error {    type Request struct {        Title string `json:"title"`    }    var body Request    err := c.BodyParser(&body)    if err != nil {        return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{            "success": false,            "message": "Cannot parse JSON",            "error":   err,        })    }    goal := &Goal{        Id:        len(goals) + 1,        Title:     body.Title,        Status: false,    }    goals = append(goals, goal)    return c.Status(fiber.StatusCreated).JSON(fiber.Map{        "success": true,        "data": fiber.Map{            "goal": goal,        },    })}
查看完整描述

1 回答

?
一只斗牛犬

TA贡献1784条经验 获得超2个赞

您的终端节点位于方法的应用程序中。但是在《邮递员》中,你叫/goals/addPOST/goals

在应用程序中期待请求。这就是为什么有方法不允许。/goalsGET


查看完整回答
反对 回复 2022-09-26
  • 1 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号