jquery - 为什么我的 PUT 请求收到 404 状态错误?

标签 jquery node.js ajax

我在 PUT 请求上收到 HTTP 404,如下所示:

enter image description here

$(function(){
$("#showMovies").click(function() {
  $.ajax({
    method:"GET",
    url: "http://localhost:3000/movielist",
    dataType: "json",
    success: function (response) {
      $.each(response, function(i, movie) {
        const rowText = "<tr>" +
          "<td>" + movie.idmovielist + "</td>" +
          "<td>" + movie.name + "</td>" +
          "<td>" + movie.thumbnail_path + "</td>" +
          "<td>" + movie.description + "</td>" +
          "<td>" + movie.year_released + "</td>" +
          "<td>" + movie.language_released + "</td>" +
          "<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
          "<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
        $("#movies").append(rowText);
      });
    }
  });
});

$("#movieAdded").click(function() {
  $.ajax({
    method:"POST",
    url: "http://localhost:3000/movielist/addMovie",
    dataType: "json",
    data: {
       idmovielist: 10,
       name: 'Bubble Gum',
       thumnail_path: 'yourieiri.jpg',
       description: 'Disturbing',
       year_released: '2007',
       language_released: 'french'
    },
    success: function (data) {
      $.each(data, function(i, movie) {
        const rowText = "<tr>" +
          "<td>" + movie.idmovielist + "</td>" +
          "<td>" + movie.name + "</td>" +
          "<td>" + movie.thumbnail_path + "</td>" +
          "<td>" + movie.description + "</td>" +
          "<td>" + movie.year_released + "</td>" +
          "<td>" + movie.language_released + "</td>" +
          "<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
          "<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
        $("#movies").append(rowText);
      });
    }
  });
});
$.ajax({
   method:"DELETE",
   url: "http://localhost:3000/movielist/8",
   dataType: "json",
   success: function (data) {
     $.each(data, function(i, movie) {
    const rowText = "<tr>" +
         "<td>" + movie.idmovielist + "</td>" +
         "<td>" + movie.name + "</td>" +
         "<td>" + movie.thumbnail_path + "</td>" +
         "<td>" + movie.description + "</td>" +
         "<td>" + movie.year_released + "</td>" +
         "<td>" + movie.language_released + "</td>" +
         "<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
         "<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
       $("#movies").append(rowText);
     });
   }
});
$.ajax({
    method:"PUT",
    url: "http://localhost:3000/movielist/6",
    dataType: "json",
    data: {
        idmovielist: 6,
        name: 'Lion King',
        thumanail_path: 'https://lumiere-a.akamaihd.net/v1/images/',
        description: 'cartoon',
        year_realeased: '2000',
        language_released: 'english'
    },
    success: function (data) {
      $.each(data, function(i, movie) {
       const rowText = "<tr>" +
          "<td>" + movie.idmovielist + "</td>" +
          "<td>" + movie.name + "</td>" +
          "<td>" + movie.thumbnail_path + "</td>" +
          "<td>" + movie.description + "</td>" +
          "<td>" + movie.year_released + "</td>" +
          "<td>" + movie.language_released + "</td>" +
          "<td>" + "<button button id = \"deleteMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Delete</button>" + "</td>" +
          "<td>" + "<button button id = \"editMovie\" type=\"button\" class=\"btn btn-danger\" data-toggle=\"modal\" data-target=\"#exampleModal\">Edit</button>" + "</td>";
        $("#movies").append(rowText);
      });
    }
});
});

因此,每当我尝试执行我的 put 请求时,我总是会收到状态代码 404 not found 的错误,而且我的 get post 和 delete 工作得很好,这只是我的 put 请求,我能做些什么来修复它。另外,我能够在我的网络浏览器中正常工作,因为我忘记添加功能

app.put('/movielist/id',(req,res) =>{
let update = req.body;
mysqlConnection.query("UPDATE movielist SET year_released = '2000' WHERE idmovielist = '6'",
[update.year_released, update.idmovielist,req.params.id],
(err, results)  => {
    if (!err) {
      res.send("Movie list is updated");
    } else {
      console.log(err);
    }
});
});

还有我的帖子请求代码

app.post('/movielist/addMovie',(req, res) => {
   mysqlConnection.query("INSERT INTO movielist (idmovielist, name, thumnail_path, description, year_released,language_released) VALUES ('10', 'Bubble Gum', 'yourieiri.jpg', 'Disturbing', '2007', 'french');",
   req.body,
   (err,rows) => {
     if (!err) {
       res.send("Movie is added");
     } else {
       console.log(err);
     }
  });
});


  app.get('/movielist',(req,res)=> {
    mysqlConnection.query("SELECT * FROM movielist", (err, rows,fields)=> {
      if (!err) {
        res.send(rows);
      } else {
        console.log(err);
      }
    });
  });
 app.delete('/movielist/:id',(req,res) => {
    mysqlConnection.query("DELETE FROM movielist WHERE idmovielist = ?",[req.params.id],(err,rows,fields) =>{
      if (!err) {
        res.send("Movie is deleted");
      } else {
      console.log(err);
    }
    });
  });
app.post('/movielist/addMovie',(req, res) => {
   mysqlConnection.query("INSERT INTO movielist (idmovielist, name, thumnail_path, description, year_released,language_released) VALUES ('10', 'Bubble Gum', 'yourieiri.jpg', 'Disturbing', '2007', 'french');",
   req.body,
   (err,rows) => {
     if (!err) {
       res.send("Movie is added");
     } else {
       console.log(err);
     }
  });
});
app.put('/movielist/:id',(req,res) =>{
let update = req.body;
mysqlConnection.query("UPDATE movielist SET year_released = '2000' WHERE idmovielist = '6'",
[update.year_released, update.idmovielist,req.params.id],
(err, results)  => {
    if (!err) {
      res.send("Movie list is updated");
    } else {
      console.log(err);
    }
});
});

Postman

最佳答案

我认为你需要改变这一点:

app.put('/movielist/id',(req,res) =>{

对此:

app.put('/movielist/:id',(req,res) =>{

在express框架的nodejs中,为了使用/link/参数/,参数需要用:声明。

关于jquery - 为什么我的 PUT 请求收到 404 状态错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55573149/

相关文章:

javascript - 在 Node.js 中使用 GM 生成动画 GIF

ajax - CRUD中的R-功能和披露漏洞之间的界线在哪里?

javascript - 选择时更改图表的数据源

jQuery post 来自 asp.net mvc3 中的外部 js 文件

javascript - Sequelize 在结果对象中混合了 Camel 大小写和蛇形大小写键

node.js - 使用express-handlebars找不到css文件

javascript - 使用 Node js 将 mp3 保存为 ajax post 数据

jquery - 如何在rails中显示Ajax错误消息?

javascript - Backbone/RequireJS 嵌套列表中的循环依赖

javascript - Coffeescript 的奇怪问题 - 文字参数发送为空