JSONP/CORS 请求


支持 JSONP 和 CORS,允许您完全在客户端代码中使用 ipinfo.io。对于 JSONP,您只需要指定回调参数,例如http://ipinfo.io/?callback=callback&token=$TOKEN


使用 Fetch API 请求访客数据 (Promise)


fetch("https://ipinfo.io/json?token=$TOKEN").then(
  (response) => response.json()
).then(
  (jsonResponse) => console.log(jsonResponse.ip, jsonResponse.country)
)


使用 Fetch API 请求访客数据(异步/等待)


const request = await fetch("https://ipinfo.io/json?token=$TOKEN")
const jsonResponse = await request.json()

console.log(jsonResponse.ip, jsonResponse.country)


使用 JSONP 请求访客数据


<script>
  function recordData (data) {
    console.log(data.ip, data.country)
  }
</script>
<script src="https://ipinfo.io/json?callback=recordData"></script>