_10_jquery_ajax.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. </body>
  16. <script>
  17. function get(){
  18. // 使用jquery 发送异步请求
  19. /**
  20. * $: 表示jquery 对象
  21. * $.get() 调用jquery 的对象额函数
  22. * get(url: 请求路径,data: 请求参数,callback: function(result){
  23. * 这个回调函数当中result 就是后端给前端返回的数据
  24. * jquery 发送请求有一个优点, 后端如果给前端返回json 数据
  25. * result-> json -> js 对象 他会帮我们把json 转成js 对象
  26. * 回调函数, 当后端处理成功就会毁掉这个callback
  27. * }))
  28. */
  29. /**
  30. * get方法请求参数 可以直接在路径当中写
  31. *
  32. * 路劲: 协议://ip:port/请求路径?请求参数名=请求参数值&请求参数名=请求参数值
  33. */
  34. var username = document.getElementById("username").value
  35. var password = document.getElementById("password").value
  36. $.get("http://c46a5489.natappfree.cc/login",{username:username,password:password},function (result) {
  37. // result 后端给前端返回的数据一般是json 的数据格式
  38. console.log(result);
  39. if(result.success){
  40. alert("登录成功")
  41. }else{
  42. alert("登录失败")
  43. }
  44. })
  45. /**
  46. * 提供一个登录界面
  47. * 发送一个get 请求把账号和密码发送请求携带到后端
  48. *
  49. */
  50. }
  51. </script>
  52. </html>