前端的PUT方法

时间:2019-04-27 01:02:23

标签: jquery crud

我正在尝试在前端发出放置请求,但该请求不起作用,也没有任何响应。

$(function(){
      function getOneMovie(id) {
              $.ajax({
                  url: "http://localhost:3000/movielist" + id,
                  method: 'GET',
                  dataType: 'json',
                  success: function (data) {
                      $($("#updateForm")[0].movieId).val(data._id);
                      $($("#updateForm")[0].intNum).val(data.intNum);
                      $($("#updateForm")[0].name).val(data.name);
                      $($("#updateForm")[0].thumnail_path).val(data.thumnail_path);
                      $($("#updateForm")[0].description).val(data.description);
                      $($("#updateForm")[0].year_released).val(data.year_released);
                      $($("#updateForm")[0].language_released).val(data.language_released);
                      $("#updateForm").show();
                  }
              });
          }

          $("#movieAdded").click(function (a) {
            console.log("movieAdded Click")
              let mydata = {
                  idmovielist: $($("#newForm")[0].intNum).val(),
                  name: $($("#newForm")[0].name).val(),
                  thumnail_path: $($("#newForm")[0].thumnail_path).val(),
                  description: $($("#newForm")[0].description).val(),
                  year_released: $($("#newForm")[0].year_released).val(),
                  language_released: $($("#newForm")[0].language_released).val(),
              }
              displayMovie(mydata);
              console.log("Hidden")
              $("#newForm").trigger("reset");
              $("#newForm").toggle();
              console.log("Hidden")
              a.preventDefault();
          });
          function displayMovie(mydata) {
              $.ajax({
                  method: "POST",
                  url: "http://localhost:3000/movielist/addMovie",
                  dataType: "json",
                  data: mydata,
                  success: function (data) {
                      console.log(data);
                  }
              });
          }

          function loadButton() {
              $(".editMovie").click(function (a) {
                  getOneMovie($($(this)[0]).data("movieId"));
                  a.preventDefault();
              });

              $(".deleteMovie").click(function (a) {
                  deleteMovie($($(this)[0]).data("movieId"));
                  a.preventDefault();
              });
          }
             loadButton();
          function putMovie(data, id) {
              $.ajax({
                  url: "http://localhost:3000/movielist/updateMovie/" + id,
                  method: 'PUT',
                  dataType: 'json',
                  data: data,
                  success: function (data) {
                    console.log(data);
                    getOneMovie();
                  }
              });
          }

          $("#updataMovie").on("click", function (a) {
              let data = {
                  idmovielist: $($("#updateForm")[0].intNum).val(),
                  name: $($("#updateForm")[0].name).val(),
                  thumnail_path: $($("#updataForm")[0].thumnail_path).val(),
                  description: $($("#updateForm")[0].description).val(),
                  year_released: $($("#updateForm")[0].year_released).val(),
                  language_released: $($("#updateForm")[0].language_released).val(),
              }

              putMovie($($("#updateForm")[0].movieId).val(), data);
              $("#updateForm").trigger("reset");
              $("#updateForm").toggle();
              a.preventDefault();

          });


          function deleteMovie(id) {
              $.ajax({
                  url: "http://localhost:3000/movielist/" + id,
                  method: 'DELETE',
                  dataType: 'json',
                  success: function (data) {
                      console.log(data);
                  }
              });
          }

});

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

以上是我在前端和后端的代码,后端由我执行的放置请求组成,当我在邮递员上执行该请求时,它可以正常工作,但是当我尝试在前端中执行该请求时,却没有任何响应我在前端做错什么了吗?

0 个答案:

没有答案