_10_jquery_ajax.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/jquery-2.1.4.js"></script>
  7. </head>
  8. <body>
  9. <form action="#" >
  10. 账号: <input type="text" name="username" id="username"><br>
  11. 密码: <input type="password" name="password" id="password"><br>
  12. <input type="button" value="登录" onclick="get()">
  13. </form>
  14. <button onclick="get()">get</button>
  15. <button onclick="post()">post</button>
  16. <button onclick="ajax()">ajax</button>
  17. </body>
  18. <script>
  19. function ajax(){
  20. /**
  21. * 在jquery 用的更多是另外ajax 方法
  22. * $.ajax({
  23. * url: 请求路径,
  24. * data: 请求参数
  25. * type: 请求方式
  26. * contentType: 请求数据格式,
  27. * dataType: 响应数据格式
  28. * success: 成功回调函数
  29. * error: 失败的回调函数
  30. * })
  31. */
  32. var username = document.getElementById("username").value
  33. var password = document.getElementById("password").value
  34. var obj = {"username":username,"password":password}
  35. $.ajax({
  36. url: "http://c46a5489.natappfree.cc/login1",
  37. data: JSON.stringify(obj), // 把传递js 对象参数转成json 字符串
  38. type: "post",
  39. dataType: "json", // 期望后端返回数据格式为json
  40. contentType:"application/json;charset=utf-8", // 前端给后端发送数据格式为json
  41. success: function (result) {
  42. if(result.success){
  43. alert("登录成功")
  44. }else{
  45. alert("登录失败")
  46. }
  47. }
  48. })
  49. }
  50. function post(){
  51. /**
  52. * 使用jquery 框架发送
  53. * $.post(url,请求参数,回调函数)
  54. */
  55. var username = document.getElementById("username").value
  56. var password = document.getElementById("password").value
  57. $.post("http://c46a5489.natappfree.cc/login",{username:username,password:password},function (result){
  58. console.log(result);
  59. if(result.success){
  60. alert("登录成功")
  61. }else{
  62. alert("登录失败")
  63. }
  64. })
  65. }
  66. function get(){
  67. // 使用jquery 发送异步请求
  68. /**
  69. * $: 表示jquery 对象
  70. * $.get() 调用jquery 的对象额函数
  71. * get(url: 请求路径,data: 请求参数,callback: function(result){
  72. * 这个回调函数当中result 就是后端给前端返回的数据
  73. * jquery 发送请求有一个优点, 后端如果给前端返回json 数据
  74. * result-> json -> js 对象 他会帮我们把json 转成js 对象
  75. * 回调函数, 当后端处理成功就会毁掉这个callback
  76. * }))
  77. */
  78. /**
  79. * get方法请求参数 可以直接在路径当中写
  80. *
  81. * 路劲: 协议://ip:port/请求路径?请求参数名=请求参数值&请求参数名=请求参数值
  82. */
  83. var username = document.getElementById("username").value
  84. var password = document.getElementById("password").value
  85. $.get("http://c46a5489.natappfree.cc/login",{username:username,password:password},function (result) {
  86. // result 后端给前端返回的数据一般是json 的数据格式
  87. console.log(result);
  88. if(result.success){
  89. alert("登录成功")
  90. }else{
  91. alert("登录失败")
  92. }
  93. })
  94. /**
  95. * 提供一个登录界面
  96. * 发送一个get 请求把账号和密码发送请求携带到后端
  97. *
  98. */
  99. }
  100. </script>
  101. </html>