我已经做了所有的步骤,通过使用ipstack.com建立IP到地理位置,并通过documentation读取。但在我的浏览器控制台为“未定义”的对象值返回。这是代码的外观:
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,因为我真的不避让的卷曲。谁能告诉我到底为什么发生这种情况?
这个问题的解决是因为我是免费订阅计划,并且不提供通过“SSL”去。只是打印,像这样JSON:成功函数内部console.log(json)
,看看它给你哪个错误。