我正在尝试使用...从 freegeoip 获取region_code...
$.getJSON('http://freegeoip.net/json/', function(location) {
console.log(location);
它返回一个对象,如下所示...
city: "Medford"
country_code: "US"
country_name: "United States"
ip: "69.142.34.172"
latitude: 39.8741
longitude: -74.809
metro_code: 504
region_code: "NJ"
region_name: "New Jersey"
time_zone: "America/New_York"
zip_code: "08055"
我需要获取区域代码,如果它是“NJ”,是否重定向到不同的 URL?
我试过这个...
var t = JSON.parse('{"city": "", "country_code": "", "country_name": "", "ip": "", "latitude": "", "longitude": "", "metro_code": "", "region_code": "", "region_name": "", "time_zone": "", "zip_code": ""}'); console.log(t['region_code']);
但在控制台中没有看到任何内容。
最佳答案
试试这个...
$.getJSON('http://freegeoip.net/json/', function(location) {
if (location.region_code == "NJ") {
window.location.href = "anotherPage.html";
}
});
假设您显示的数据就是返回的数据,那么这将满足您的需要。
关于javascript - 从ip获取状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43188241/