2017-01-22 56 views
1

我想通過我的json數據location參數assignLocationValuesconsole.log它。當我登錄location.loc它顯示了兩個值,如下所示:4,2394,5,3023。當我從它的價值如:location.loc[1]並記錄它。價值變化,而不是5,3023它變爲1.發生了什麼?爲什麼我的JSON數據改變了?

編輯:弄明白了。我真的認爲這是一個數組。太奇怪了...............

$(document).ready(function() { 
getLocation(); 

function getLocation() { 

    $.get("http://ipinfo.io", function(location) { 

    assignLocationValues(location.loc[0], location.loc[1], location.city, location.region, location.country); 

    function assignLocationValues(lat, lon, city, region, country){ 

     console.log(location.loc); 
     console.log(lat); 
    }; 
    }, 'jsonp'); 
}; 
}); 

回答

0

將其放入在標籤底部的腳本標籤(的document.ready是昨天的新聞)

function assignLocationValues(obj_location){ 

    var loc = obj_location; 

    var _city = loc.city; 
    var _country = loc.country; 
    var _hostname = loc.hostname; 
    var _ip = loc.ip; 
    //.... 
    console.log(_ip); 

}; 

function getLocation() { 
    $.get("http://ipinfo.io/json", function(location) { 
     assignLocationValues(location); 
    }); 
} 

getLocation(); 
-1

試試這個:http://jsbin.com/IDebUfu/1/edit?html,js,output

$.getJSON("http://ip-api.com/json/?callback=?", function(data) { 
    var table_body = ""; 
    $.each(data, function(k, v) { 
    table_body += "<tr><td>" + k + "</td><td><b>" + v + "</b></td></tr>"; 
    }); 
    $("#GeoResults").html(table_body); 
}); 
相關問題