2017-02-14 32 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