2015-03-08 112 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做链接,但希望得到您领导的正确途径。