ajax的getJSON()方法_ajax get json

语法

$.getJSON(url,data,function(data){
	······
})
  • 1
  • 2
  • 3

url:要请求的地址
data:发送给服务器的数据,为键值对格式
function(data){}:表示请求成功后的回调函数,请求失败是不会处理的

应用实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/jquery-3.6.0.js"></script>
</head>
<body>
    
    <h3>这是一次ajax尝试</h3>
    <span id="ax"></span>

    <script>
        $.getJSON('https://v1.hitokoto.cn/?encode=json',function(data){
            var s=$("#ax");
            s.text(data.hitokoto);
        })
    </script>

</body>
</html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述