当用户不从西班牙浏览时,将用户重定向到另一个页面

时间:2016-09-26 10:14:53

标签: javascript ajax web freegeoip

感谢您的帮助。当我尝试通过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给我的信息。谁知道为什么?

1 个答案:

答案 0 :(得分:-1)

使用type:'GET'

jQuery.ajax( { url: 'https://www.freegeoip.net/json/', type: 'GET', 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. location.href = 'http://theInternationalWeb.com'; } } }});