【问题标题】:Ajax:unable to POST (404)Ajax:无法发布 (404)
【发布时间】:2020-03-19 20:17:07
【问题描述】:

manage.ejs 文件中的 POST 脚本我在控制台记录了它在那里工作的数据并

<script type="text/javascript">
  var guildID = "<%= guild.id %>";
  let data = {some valid json}
  $('.btn-primary.btn').click(function() {
            $.ajax({
                url: `/dashboard/${guildID}/manage/save`,
                type: "POST",
                dataType: "json",
                data: JSON.stringify(data),
                contentType: "application/json",
                cache: false,
                timeout: 5000,
                complete: function() {
                  //called when complete
                  console.log('process complete');
                },

                success: function(data) {
                  console.log(data);
                  console.log('process sucess');
               }
              });
        })
</script>

dashboard.js 我导入了 body-parser 我在 /save 中遗漏了一些东西吗?其他所有帖子也都在工作

 var bodyParser = require("body-parser");
  app.use(bodyParser.json());       // to support JSON-encoded bodies
  app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
    extended: true
  })); 

app.get("/dashboard/:guildID/save", checkAuth, async (req, res) => {
    const guild = client.guilds.get(req.params.guildID);
    if (!guild) return res.status(404);
    res.contentType('json');
   console.log(req.body)

    res.redirect("/dashboard/"+req.params.guildID);
  });

【问题讨论】:

  • 看起来您正在发帖到:/dashboard/${guildID}/manage/save,但只收听/dashboard/:guildID/save

标签: javascript node.js ajax express post


【解决方案1】:

您发布到:/dashboard/${guildID}**/manage/save**,但服务于端点 /dashboard/:guildID**/save**

【讨论】:

    猜你喜欢
    • 2018-11-26
    • 2021-02-24
    • 2011-05-21
    • 2015-04-24
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    相关资源
    最近更新 更多