我已经完成使用ipstack.com设置ip到地理位置的所有步骤,并通读了documentation。但是对象值在我的浏览器控制台中返回为“ undefined”。代码是这样的:
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。谁能确切告诉我为什么会这样?
答案 0 :(得分:0)
此问题的解决方案是因为我处于免费订阅计划中,并且不提供通过“ ssl”进行访问的计划。只需像这样打印json:在成功函数内==25999== Conditional jump or move depends on uninitialised value(s)
==25999== at 0x4ECC374: _IO_str_init_static_internal (strops.c:51)
==25999== by 0x4EBD6C2: vsprintf (iovsprintf.c:41)
==25999== by 0x4EA1093: sprintf (sprintf.c:32)
==25999== by 0x4008E7: read_data
==25999== by 0x400B80: main
==25999== Uninitialised value was created by a heap allocation
==25999== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25999== by 0x400898: read_data
==25999== by 0x400B80: main
,然后查看它会给您带来什么错误。