http://ip.tanwan.com/index.php?action=ipinfo&format=js
如何利用ajax获取到里面的这些数据然后再赋值到html中的元素当中?
http://ip.tanwan.com/index.php?action=ipinfo&format=js
如何利用ajax获取到里面的这些数据然后再赋值到html中的元素当中?
// 此处可按需求封装修改
const getRequest = () => {
return new Promise((resolve, reject) => {
const x = new XMLHttpRequest();
x.open("GET", url);
x.send();
x.onreadystatechange = function () {
if (x.readyState === 4) {
if (x.status >= 200 && x.status < 300) {
//成功啦
resolve(x.response);
} else {
//如果失败
reject(x.status);
}
}
};
});
};
const renderHtml =async () => {
// data 就是 {"ret":1, "country":"中国", "province":"北京", "city":"北京"}
const data = await getRequest()
const pageDom = document.getElementById('需要将数据渲染到页面的标签 id')
const str = `<div>
<span>转发:${data.ret}</span>
<span>国家:${data.country}</span>
<span>省份:${data.province}</span>
<span>城市:${data.city}</span>
</div>`
pageDom.appendChild(str)
}
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
3 回答1.9k 阅读✓ 已解决
请求类型为document不能用ajax, 回答中的方法就类似CDN引入比如:


<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>