$.get
是采用了 GET 方式请求接口。
格式如下
$.get( url, [, data] [, callback] [, type])
参数名称 | 类型 | 说明 |
---|---|---|
url | String | 请求 HTML 页面的 URL 地址 |
data(可选) | Object | 发送至服务器的 key/value 数据 |
callback(可选) | Function | 回调函数,只有当请求成功才执行! |
type(可选) | String | 服务器返回的数据格式,包括 xml/html/json/text 等 |
<html>
<head>
<title>example</title>
<meta charset="utf-8"/>
</head>
<body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
$(function () {
$.get("https://clwy.cn/api/v2/home.json", {}, function (data) {
console.log(data)
}, "json")
});
</script>
</body>
</html>
如果获取的数据,确定是 json 格式,也可以直接使用$.getJSON
方法
<html>
<head>
<title>example</title>
<meta charset="utf-8"/>
</head>
<body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
$(function () {
$.getJSON("https://clwy.cn/api/v2/home.json", {}, function (data) {
console.log(data)
})
});
</script>
</body>
</html>
已添加到喜欢了