You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过外部API及qx.io.request.Xhr获取客户端公网IP与地理位置

Hey there! Let me walk you through how to get this working with qx.io.request.Xhr using one of those IP APIs—let's start with ip-api.com first since it's free for non-commercial use, doesn't require an API key for basic requests, and plays nicely with browser CORS rules.

Fetch Public IP & Geolocation with qx.io.request.Xhr

Working Example with ip-api.com

Here's a fully functional implementation that should resolve your issues:

// Initialize a new XHR request instance targeting ip-api's JSON endpoint
const ipRequest = new qx.io.request.Xhr("http://ip-api.com/json/");

// Set response type to JSON to auto-parse the result (no manual JSON.parse needed)
ipRequest.setResponseType("json");

// Handle successful response
ipRequest.addListener("success", (event) => {
  const responseData = event.getTarget().getResponse();
  
  // Log full response for debugging
  console.log("Full IP & Location Data:", responseData);
  
  // Access specific fields easily
  console.log("Public IP:", response.query);
  console.log("City:", responseData.city);
  console.log("Country:", responseData.country);
  console.log("Coordinates:", `${responseData.lat}, ${responseData.lon}`);
});

// Handle errors (network issues, rate limits, etc.)
ipRequest.addListener("error", (event) => {
  console.error("Failed to fetch IP data:", event.getTarget().getStatusText());
});

// Send the request to the API
ipRequest.send();

Why Your Previous Attempt Might Have Failed

  • Missing Response Type: If you didn't set responseType to "json", you'd have to manually parse the raw text response, which can lead to syntax errors if the response isn't perfectly formatted.
  • CORS Misconfiguration: Some APIs require specific headers, but ip-api.com and ipapi.co both support unauthenticated browser requests out of the box.
  • Incorrect Endpoint: Double-check the API URL structure—for example, ipapi.co uses https://ipapi.co/json/ instead of the ip-api format.

Alternative: Using ipapi.co

If you prefer ipapi.co, here's the adjusted code (works without an API key for limited free requests):

const ipRequest = new qx.io.request.Xhr("https://ipapi.co/json/");
ipRequest.setResponseType("json");

ipRequest.addListener("success", (event) => {
  const data = event.getTarget().getResponse();
  console.log("IP:", data.ip);
  console.log("Full Location:", `${data.city}, ${data.region}, ${data.country_name}`);
});

ipRequest.addListener("error", (event) => {
  console.error("Request failed:", event.getTarget().getStatusText());
});

ipRequest.send();

Quick Notes on Rate Limits

  • ip-api.com allows up to 45 requests per minute from a single IP for non-commercial use.
  • ipapi.co has a free tier with 1,000 requests per day—if you need more volume, you'll need to sign up for an API key.

Just drop this code into your qooxdoo project, and it should pull the client's public IP and location data smoothly.

内容的提问来源于stack exchange,提问作者R. Islas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:25:20