ajax简单格式:

注意写法:$.ajax({}) - 静态的函数

  1. $.ajax({
  2. url:"http://192.168.2.182:8080/check",//请求地址
  3. type:"get",//请求类型
  4. data:{//请求数据
  5. userName:$(this).val()
  6. },
  7. success:(resp)=>{//响应成功:获得数据
  8. console.log(this)
  9. },
  10. fail:function(err){//响应失败:返回异常
  11. console.log(err)
  12. }
  13. })
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <script src="https://lib.baomitu.com/jquery/3.5.1/jquery.min.js"></script>
  8. </head>
  9. <body>
  10. <form action="">
  11. <div class="item">
  12. <lable>姓名</lable>
  13. <input type="text" name="userName">
  14. <span></span>
  15. </div>
  16. </form>
  17. <script>
  18. $(function(){
  19. $('[name="userName"]').blur(function(){
  20. $.ajax({
  21. url:"http://192.168.2.182:8080/check",//请求地址
  22. type:"get",//请求类型
  23. data:{//请求数据
  24. userName:$(this).val()
  25. },
  26. success:(resp)=>{//响应成功:获得数据
  27. console.log(this)
  28. },
  29. fail:function(err){//响应失败:返回异常
  30. console.log(err)
  31. }
  32. })
  33. })
  34. })
  35. </script>
  36. </body>
  37. </html>

注意

function(){}匿名函数和()=>{}箭头函数的区别(this的指向)

匿名函数是带上下文的,箭头函数不带
匿名函数里this的指向是外层函数;箭头函数里this的指向是自己外层函数的外层函数