码迷,mamicode.com
首页 > Web开发 > 详细

获取到ajax异步请求的数据的方法

时间:2017-03-24 13:18:41      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:function   success   fail   初次打开   and   bll   ack   nbsp   方法   

// 通过GPS坐标取城市名
function getCityNameByLocation(lng, lat, callback) {
// 参考:http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
$.ajax({
url: ‘//api.map.baidu.com/geocoder/v2/‘,
type: ‘GET‘,
data: {
ak: ‘eNb809Xt5UBLLxCGKkmj6IOdEfwQyhwM‘,
coordtype: ‘wgs84ll‘,
location: lat + ‘,‘ + lng,
output: ‘json‘
},
dataType: ‘jsonp‘,
success: function(res) {
var cityName = ‘‘;
if (res && res.status === 0 && $.isPlainObject(res.result) && $.isPlainObject(res.result.addressComponent) && res.result.addressComponent.city) {
cityName = res.result.addressComponent.city.replace(‘市‘, ‘‘);
}
callback.call(this, cityName);
}
});
}

 

 

function init() {
if ($.cookie(‘gps_cache‘)) {
return;
}
getGeoLocation(function(lng, lat) {
getCityNameByLocation(lng, lat, function(cityName) {
if (cityName) {
setGpsCacheCountdown();
setCity(cityName, lastGpsCity);
} else {
getLocationFailedHandler();
}
});
});
}

init();

 

// 设置城市
function setCity(gpsCity, lastGpsCity) {
$.cookie(‘gps_city‘, gpsCity, {
expires: 365,
path: ‘/‘
});

if (lastGpsCity) {
// 定位城市发生变化
if (lastGpsCity !== gpsCity) {
switchCity(gpsCity);
}
} else {
// 初次打开首页时,定位城市和显示城市不同
var shownCity = $.cookie(‘shown_city‘);
if (shownCity && shownCity !== gpsCity) {
switchCity(gpsCity);
}
}
}

 

获取到ajax异步请求的数据的方法

标签:function   success   fail   初次打开   and   bll   ack   nbsp   方法   

原文地址:http://www.cnblogs.com/allenda/p/6610892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!