2017-02-14 77 views
0

你能让我明白为什么这段代码似乎无法正常工作吗?基于国家的简单jquery重定向工作不正常

<html><head><title></title> 
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> 
<script> 
jQuery.ajax({ 
    url: '//freegeoip.net/json/', 
    type: 'POST', 
    dataType: 'jsonp', 
    success: function(location) { 
    // If the visitor is browsing from Romania or GB 
    if (location.country_code === 'RO' || 'GB') { 
     // Redirect him to the Canadian store. 
     window.top.location.href = 'http://shop-in-canada.myshopify.com'; 
    } 
else 
{ return false; } 
    } 
}); 
</script> 
</head><body></body></html> 

通过适当我的意思是,这重定向我的shop-in-canada.myshopify.com连我都GB或RO或美国或CA或任何其他国家。你认为这个问题来自哪里?

+0

你能尝试用下面这样的'''dataType''','''数据:JSON.stringify''' – FreedomPride

+0

本文归“的XMLHttpRequest无法加载http://freegeoip.net/json/。所请求的资源上没有'Access-Control-Allow-Origin'标头,因此Origin'site .com'不允许访问,响应的HTTP状态码为405。在控制台中。但感谢您的帮助。 @saravanan答案帮助我所有我需要的! – Adrian

回答

0

我想你应该更换

if (location.country_code === 'RO' || 'GB') { 
    // Redirect him to the Canadian store. 
    window.top.location.href = 'http://shop-in-canada.myshopify.com'; 
} 

if (location.country_code === 'RO' || location.country_code === 'GB') { 
    // Redirect him to the Canadian store. 
    window.top.location.href = 'http://shop-in-canada.myshopify.com'; 
} 

对于多次检查,请

var language = ["RO", "GB"]; 
if (language.indexOf(location.country_code) !== -1) { 
    // Redirect him to the Canadian store. 
    window.top.location.href = 'http://shop-in-canada.myshopify.com'; 
} 
+0

这工作。谢谢,但这里没有任何方法可以不重复location.country_code每一次? – Adrian

+0

我已经更新了答案。请检查 –

+0

伟大的,多数民众赞成真棒。非常感谢。 – Adrian

1
if (location.country_code === 'RO' || 'GB') 

将无法​​工作。记录该线路,您不会收到truefalse,而是您收到"GB"

只需使用

if (location.country_code === 'RO' || location.country_code === 'GB')

更换上面会做的伎俩。

+0

只是一个参考:使用该网站可能不是你最好的选择,因为我有一个广告拦截器,而且如果不先禁用它,它就不适用于我。 – FibreChips

+0

对于我用来不被adblock阻止的建议是什么? @fibrechips – Adrian

+1

可以向freegeoip发送一个IP地址:https://freegeoip.net/json/xxx.xx.xx.xxx - 只需从服务器发送客户端的IP地址,然后根据响应重定向,而不是做它在客户端。 – AmericanUmlaut

0

那么,你可以尝试这种方式,你必须做的每个国家的代码。

jQuery.ajax({ 
       url: '//freegeoip.net/json/', 
       type: 'POST', 
       dataType: 'jsonp', 
       success: function(location) { 
        if (location.country_code === 'US') { 
         // do nothing 
        } 
        else if (location.country_code === 'AU') { 
         window.location.href = 'http://yoururlhere.com'; 
        } 
        else if (location.country_code === 'SE') { 
         window.location.href = 'http://yoururlhere.com'; 
        } 
        else if (location.country_code === 'NL') { 
         window.location.href = 'http://yoururlhere.com'; 
        } 
        else if (location.country_code === 'GB') { 
         // window.location.href = 'http://yoururlhere.com'; 
        } 
       } 
      }); 
+0

谢谢,但对我来说更实用,如果(var ='country1'|| var ='country2'|| var ='country3'|| var ='country4'|| var ='countr5y'|| var ='country6'|| var ='country7'),因为我尝试将所有这些国家重定向到单个网址,其余的转到其他网址。 – Adrian