与以下问题有关。
我正在寻求一些关于如何确保从 ajax 调用中获得所有结果的指导。
特别是,这一次的问题在于getPublicIPAddress()
函数及其 ajax 调用。
function genericYiiLocation(callback) {
//console.log('genericYiiLocation: Creating location handler');
this.longitude= '';
this.latitude= '';
this.accuracy= '';
this.publicIpAddress= '';
var me = this;
if (Modernizr.geolocation) {
//console.log('genericYiiLocation: Location supported');
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locateSuccess, locateFail);
} else {
alert('genericYiiLocation: Geolocation is not supported in your current browser.');
callback(false);
}
} else {
alert ('genericYiiLocation: no native location support');
callback(false);
}
GetPoo();
function GetPoo(getPublicIPAddress){
console.log(this.accuracy);
}
function locateSuccess(loc){
// console.log('genericYiiLocation: storing location data');
me.longitude = loc.coords.longitude;
me.latitude = loc.coords.latitude;
me.accuracy = loc.coords.accuracy;
callback(true, me);
}
// Unsuccessful geolocation
function locateFail(geoPositionError) {
switch (geoPositionError.code) {
case 0: // UNKNOWN_ERROR
alert('An unknown error occurred, sorry');
break;
case 1: // PERMISSION_DENIED
alert('Permission to use Geolocation was denied');
break;
case 2: // POSITION_UNAVAILABLE
alert('Couldn\'t find you...');
break;
case 3: // TIMEOUT
alert('The Geolocation request took too long and timed out');
break;
default:
}
callback(false, geoPositionError);
}
function getPublicIPAddress(callback)
{
var ip;
$.ajax({
type: 'GET',
url: 'http://smart-ip.net/geoip-json?callback=?',
dataType: 'json',
async: true ,
success: function(data) {
ip = data.host;
callback(false,ip);
}
});
//callback();
}
}