从ip获取状态

时间:2017-04-03 15:11:13

标签: javascript json geolocation

我试图使用...来获取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"

我需要获取region_code,如果是" NJ",请重定向到其他网址吗?

我试过了......

var t = JSON.parse(' {" city":""," country_code":"" ," country_name":""," ip":"","纬度":" ","经度":""," metro_code":""," region_code": ""," region_name":""," time_zone":""," zip_code& #34;:""}'); 的console.log(T [' REGION_CODE']);

但是在控制台里什么都没看到。

1 个答案:

答案 0 :(得分:1)

试试这个......

$.getJSON('http://freegeoip.net/json/', function(location) {
    if (location.region_code == "NJ") {
        window.location.href = "anotherPage.html";
    }
});

假设您显示的数据是返回的数据,那么这将满足您的需求。