1. <!DOCTYPE html>
    2. <html lang="zh">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>TestAjax</title>
    6. <link crossorigin="anonymous" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.1/css/bootstrap.css" rel="stylesheet">
    7. <script crossorigin="anonymous" src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.js"></script>
    8. </head>
    9. <body>
    10. <div class="container">
    11. <h2 class="page-header">jQuery发送Ajax请求</h2>
    12. <button class="btn btn-primary">发送GET请求</button>
    13. <button class="btn btn-danger">发送POST请求</button>
    14. <button class="btn btn-info">发送通用(ALL)请求</button>
    15. </div>
    16. <script>
    17. // get请求
    18. $('button').eq(0).click(function (){
    19. $.get('http://localhost:8000/jQuery','{a:100,b:200}',function (data){
    20. console.log(data);
    21. });
    22. });
    23. // post请求
    24. $('button').eq(1).click(function (){
    25. $.post('http://localhost:8000/jQuery','{a:100,b:200}',function (data){
    26. console.log(data);
    27. },'json');
    28. });
    29. // 通用方法请求
    30. $('button').eq(2).click(function (){
    31. $.ajax({
    32. url : 'http://localhost:8000/jQuery',
    33. data: {a:100,b:200},
    34. type: 'GET',
    35. dataType: 'json',
    36. success: function (data){
    37. console.log(data);
    38. },
    39. timeout: 2000,
    40. error: function (){
    41. alert("出错啦")
    42. },
    43. headers: {
    44. c: 300,
    45. d: 400,
    46. }
    47. });
    48. });
    49. </script>
    50. </body>
    51. </html>