我已经完成了使用ipstack.com设置IP地址到地理位置的所有步骤,并阅读了文档。但是在我的浏览器控制台中,对象值以“未定义”返回。以下是代码:
var ip = '188.124.3.1';
var access_key = 'secret';
$.ajax({
url: 'https://api.ipstack.com/' + ip + '?access_key=' + access_key,
dataType: 'jsonp',
success: function(json) {
var city = json.city;
var zip = json.zip;
var latitude = json.latitude;
var longitute = json.longitute;
console.log(city + ' ' + zip + ' ' + latitude + ' ' + longitute);
console.log('success!');
//the code reaches this stage, but returns as 'undefined' for each value above
//execute a second ajax function to insert the values into a database.
$.ajax({
type: "post",
url: "insert_geolocation.php",
data: {
'zip': zip,
'lat': latitude,
'lng': longitute
},
success: function (data) {
// it's successful, do nothing else.
// the code also reaches this stage, but the database gets a row with NULL and 0 as values...So the response from the API is returning undefined, for an unknown reason.
},
});
},
error: function(html, data) {
console.log('Error!');
console.log(data);
}
});
我正在使用jQuery和文档中的JSON输出,因为我并不太了解CURL。有人能告诉我为什么会发生这种情况吗?