谢谢你的帮助。当我尝试通过 Freegeoip 根据用户的位置重定向用户时,我遇到了一些麻烦。
可能对你来说很简单,但我无法让它工作,行为应该是:
所有访问者都访问西班牙网站 www.theWeb.es,但那些从西班牙境外浏览的人应该被重定向到国际页面 www.theInternationalWeb.com。
代码:
<script>
jQuery.ajax( { url: 'https://www.freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
if (location.country_code === 'ES') {
// Do nothing because the user is already in the spanish Store.
else {
// if the user is not in Spain the send him to the international store.
window.top.location.href = 'http://theInternationalWeb.com';
}
} }});
</script>
谢谢你的时间。
附加信息:
国际网站还将西班牙用户重定向到西班牙网站,而且它似乎做得很好,因为它每次都会重定向我(我位于西班牙)
<script>
jQuery.ajax( {
url: 'https://www.freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
if (location.country_code === 'ES') {
// Redirect him to the Spanish store.
window.top.location.href = 'http://myWeb.es';}
}} );
</script>
重要的:
我检查了代码并发出警报以检测我拥有的 location.country_code ,似乎 jsonp 给了我法国国家代码 FR 而不是西班牙的 ES。所以问题不在于代码,而在于 FreeGeoIP 给我的信息。有谁知道为什么?