仅在前端执行发布请求

时间:2019-04-17 14:12:22

标签: javascript html crud

URL url = new URL("my url ");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
System.out.println(body);


public static List<String> readFileInList(String myFileName) 
 { 
   List<String> lines = Collections.emptyList(); 
   try
   { 
     lines = 
      Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8); 
   } 

   catch (IOException e) 
   { 

     e.printStackTrace(); 
   } 
   return lines; 
 } 
public static void main(String[] args) throws FileNotFoundException

   List l = readFileInList("my file"); 
   char board[][] = {
             {'X','A','G','L','K'},
             {'Y','U','P','T','K'},
             {'Z','Y','N','X','M'},
             {'Q','G','E','E','B'},
             {'R','O','A','P','Q'}};
   System.out.println("");
   Search(board);
$(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(a) {
        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);
        $("#newForm").trigger("reset");
        $("#newForm").toggle();
        a.preventDefault();
      });

      function displayMovie(data) {
        $.ajax({
          method: "POST",
          url: "http://localhost:3000/movielist/addMovie",
          dataType: "json",
          data: data,
          success: function(data) {
            console.log(data);
          }
        });
      }
      $.ajax({
        method: "DELETE",
        url: "http://localhost:3000/movielist/5",
        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);
          });
        }
      });
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

table {
  background-color: lightblue;
}

tbody {
  font-family: inherit;
}

html {
  background-color: lightblue;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

enter image description here

所以上面是我的html css和java-script,还有我的UI的屏幕截图,我做了一个发帖请求后端,当我单击添加按钮时,我也在前端执行了它模式窗体弹出我要添加的内容,这是一种方法,我可以只在前端执行发布请求,而不必回到后端并每次更改

<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <link href="mystyle.css" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
  <script src="mycrud.js"></script>
</head>

<body>
  <title>My Movies</title>
  <header>
    <h1>Movies</h1>
    <button id="showMovies" type="button" class="btn btn-primary" data-toggle="modal" data-target=#exampleModal>All Movies</button>
  </header>
  <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
        </div>

        <div class="modal-body">
          <form id="newForm">
            <div class="form-group row">
              <label for="idmovielist" class="col-sm-2 col-form-label">idmovielist</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="intNum" placeholder="idmovielist">
              </div>
            </div>
            <div class="form-group row">
              <label for="name" class="col-sm-2 col-form-label">name</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="name" placeholder="name">
              </div>
            </div>
            <div class="form-group row">
              <label for="thumnail_path" class="col-sm-2 col-form-label">thumnail_path</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="thumnail_path" placeholder="thumnail_path">
              </div>
            </div>
            <div class="form-group row">
              <label for="description" class="col-sm-2 col-form-label">description</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="description" placeholder="description">
              </div>
            </div>
            <div class="form-group row">
              <label for="year_released" class="col-sm-2 col-form-label">year_released</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="year_released" placeholder="year_released">
              </div>
            </div>
            <div class="form-group row">
              <label for="language_released" class="col-sm-2 col-form-label">language_released</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="language_released" placeholder="language_released">
              </div>

            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              <button id="movieAdded" type="button" class="btn btn-primary" data-toggle="modal" data-target=#exampleModal>Add</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>

  <button id="movieAdded" type="button" class="btn btn-primary" data-toggle="modal" data-target=#exampleModal>Add</button>
  <table class="table table-bordered table-hover" width="100%">
    <thead style="background-color:#ddd;" class="table-borderless">
      <tr>
        <th>idmovielist</th>
        <th>name</th>
        <th>thumnail_path</th>
        <th>description</th>
        <th>year_released</th>
        <th>language_released</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody id="movies">
    </tbody>
  </table>
  </header>
</body>

</html>

上面是我做的后端代码,您在Ui中看到的是结果,但是如果我想添加更多电影,是否可以直接在前端添加电影而不必一直返回到后端进行更改

2 个答案:

答案 0 :(得分:0)

mysqlConnection.query("INSERT INTO movielist (`idmovielist`,`name`,`thumnail_path`,`description`,`language_released`,`year_released`) VALUES ('64','DudeLove','wfburfr.jpg','silly','2019','english')",

您已经硬编码了要插入查询中的值。

不要那样做。 Read the values from the POST request

答案 1 :(得分:0)

在使用POST方法时,必须在POST正文中传递参数并在服务器端读取它们。
您的客户端是正确的-您将data传递到服务器。

$.ajax({
   method: "POST",
   url: "http://localhost:3000/movielist/addMovie",
   dataType: "json",
   data: data,
   success: function(data) {
      console.log(data);
   }
});

在服务器端,您使用req.body.PARAMNAME读取POST正文数据元素;
当您将它们作为JSON传递时,它们将已经被解析。
现在,您只需将这些值动态地放入查询中即可:

"INSERT INTO movielist (`idmovielist`,`name`) VALUES ('"+req.body.idmovielist+"', '"+req.body.name+"')",

课程,您应先转义SQL injections的字符串。而且ID可能不应该传递,而应该传递auto-incremented在数据库中。