test.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  7. </head>
  8. <script>
  9. function send1() {
  10. let id = $("#id").val();
  11. let name = $("#name").val();
  12. // $.ajax({
  13. // //url
  14. // url: 'test2',
  15. // //参数
  16. // data: { a: 100, b: 100 },
  17. // //请求参数
  18. // type: 'post',
  19. // //响应体结果
  20. // dataType: 'json',
  21. // contentType: 'application/json;charset=UTF-8',
  22. // //成功的回调
  23. // success: function (data) {
  24. // res = JSON.stringify(data);
  25. // console.log(data);
  26. // },
  27. // error: function () {
  28. // console.log('出错了!!');
  29. // }
  30. // });
  31. $.ajax({
  32. url: "test2",
  33. type: "post",
  34. //data表示发送的数据
  35. data: JSON.stringify({username: id, password: name}),
  36. dataType: 'json',
  37. contentType: 'application/json;charset=UTF-8',
  38. success: function (data) {
  39. // alert(data)
  40. // // alert(JSON.stringify(data));
  41. $('.result').text(JSON.stringify(data));
  42. // for (let i = 0; i < data.length; i++) {
  43. // $('.result').text(data[i].username);
  44. // }
  45. }
  46. });
  47. }
  48. </script>
  49. <body>
  50. <form>
  51. 学号:<input id="id" type="text"><br>
  52. 姓名:<input id="name" type="text"><br>
  53. <input type="button" value="提交json数据" onclick="send1()">
  54. </form>
  55. <div class="result"></div>
  56. </body>
  57. </html>