【发布时间】:2018-01-19 01:27:23
【问题描述】:
我正在学习 JavaScript 和 Ajax。我不想使用任何库或框架。
我找到了一个使用这个的代码:https://codepen.io/Aurelian/pen/VyqemX
我的问题是,如何使用纯 JavaScript 实现这一点?没有 jQuery?
我需要重新学习 AJAX 基础知识,因为我已经很久没有这样做了。
另外,我需要连接到 API 吗?网址部分?其他网站如何获取当前位置?
有类似的问题,但他们都是 jQuery。所以请使用 Vanilla JavaScript。
JS代码:
jQuery.ajax( {
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
// example where I update content on the page.
jQuery('#city').html(location.city);
jQuery('#region-code').html(location.region_code);
jQuery('#region-name').html(location.region_name);
jQuery('#areacode').html(location.areacode);
jQuery('#ip').html(location.ip);
jQuery('#zipcode').html(location.zipcode);
jQuery('#longitude').html(location.longitude);
jQuery('#latitude').html(location.latitude);
jQuery('#country-name').html(location.country_name);
jQuery('#country-code').html(location.country_code);
}
} );
【问题讨论】:
标签: javascript json ajax geolocation