2015-03-08 138 views
-2

所以我沒有很多Javascript的經驗,但是我在過去做得很好。我有這個網站,我試圖運行'geoip'來拉兩個iso到一個特定的頁面,我已經得到完美的功能。然而,如果所述ISO完成購買,則向他們呈現謝謝頁面,但是因爲「geoip」仍然存在而消失。我試過運行一個'返回',但似乎無法得到它的竅門。在崇高的文本3看起來很好,任何幫助將非常感激。這裏是我的代碼:SyntaxError:意外的令牌else

$.ajax({ 
    url: 'https://freegeoip.net/json/', 
    type: 'POST', 
    dataType: 'jsonp', 
    success: function(location) { 
     // If the visitor is browsing from Australia or New Zealand. 
     if (location.country_code == 'AU' || location.country_code == 'NZ') { 
      // Redirect visitor to the purchasing store. 
      window.location.href = 'http://example.com/order-international/'; 
      else { 
       location.country_code == 'AU' || location.country_code == 'NZ' === window.location.href = 'http://example.com/thanks-international/') { 
       return; //stop the execution of function 
      } else { 
       //keep on going 
      } 
     } 
    } 
}); 
+5

歡迎來到Stack Overflow!正如標題文本框中的佔位符所建議的,您的問題的標題應該是問題的簡短版本,而不是「請幫助我」或類似內容。 – 2015-03-08 18:21:07

+1

我已經清理了您的代碼格式。看看結果,我懷疑你有一對錯位的「{}」(至少,似乎還有其他問題)。 – 2015-03-08 18:24:48

+1

使用'GET' for jsonp – charlietfl 2015-03-08 18:24:49

回答

2

兩件事情:

首先,當我清理你的代碼格式,它變得相當清楚,有一些錯位的{}(至少)。我試着從評論中猜出你下面真的想要做什麼。

其次,你說過你想停止執行「函數」,所以我猜這個ajax調用在某個函數中。

你所做的就是使功能的其餘部分成爲一個功能,然後調用從ajax成功處理程序,如果你想它運行,或不,如果你不這樣做。例如: -

function yourOuterFunction() { 
    $.ajax({ 
     url: 'https://freegeoip.net/json/', 
     type: 'POST', 
     dataType: 'jsonp', 
     success: function(location) { 
      // If the visitor is browsing from Australia or New Zealand. 
      if (location.country_code == 'AU' || location.country_code == 'NZ') { 
       // Redirect visitor to the purchasing store. 
       window.location.href = 'http://example.com/order-international/'; 
      } else { 
       // keep going 
       keepGoing(); 
      } 
     } 
    }); 

    function keepGoing() { 
     // The part you want to do after you have the ajax result 
    } 
} 

我也不太知道什麼是你想用你的thanks-international做鏈接,但希望得到您領導的正確途徑。