请问这个如何js输出?
这个js直接打开的话<script src="https://restapi.amap.com/v3/ip?key=2004f145cf3a39a72e9ca70ca4b2a1dc"></script>
显示如下:{"status":"1","info":"OK","infocode":"10000","province":"广东省","city":"广州市","adcode":"440100","rectangle":"113.1017375,22.93212254;113.6770499,23.3809537"}
我希望用document.getElementById("city").innerHTML得到city的值,该如何写?谢谢。
回复
1个回答
test
2024-07-05
这个本来就是接口 才会出现你看的样子, 可以直接请求的
<!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>
</head>
<body>
<div class="city"></div>
</body>
<!-- <script src="https://restapi.amap.com/v3/ip"></script> -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
selectTeacher()
function selectTeacher (empName) {
$.ajax({
url: "https://restapi.amap.com/v3/ip",
type: "get",
dataType: "json",
data: "key=2004f145cf3a39a72e9ca70ca4b2a1dc",
success: function (result) {
console.log(result);
document.querySelector(".city").innerHTML = `+${result.city}+${result.province}+`
},
error: function (result) { alert(result.message) }
});
}
</script>
</html>
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容