为什么我的PUT请求出现404状态错误?

时间:2019-04-08 12:08:50

标签: 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);
      });
    }
});
});

因此,每当我尝试执行我的放置请求时,总是会收到找不到状态码404的错误,并且我的发布和删除工作也很好,这只是我的放置请求,我可以做什么来解决它。另外,由于我忘记添加功能,因此我也可以在Web浏览器中使用它

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

1 个答案:

答案 0 :(得分:0)

I think you need to change this:

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

to this:

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

In nodejs with express framework, in order to use /link/parameter/, parameter needs to be declared with :.