Javascript ajax没有';第一次单击时不会更新

Javascript ajax没有';第一次单击时不会更新,javascript,jquery,ajax,cookies,Javascript,Jquery,Ajax,Cookies,我的代码有问题,就像一个按钮。它显示了喜欢的数量。若用户还并没有投票(cookie),他可以点击并计数器增加。问题是计数器并没有在第一次点击时更新(若我停用cookie检查并多次投票),在下一次刷新时一切都更新了。似乎在后端插入之前发生了一些计数。我假设probem在JavaScript中,ajax post跨域工作,但给出了错误,这就是为什么“error:setCookie和updateButton() 这是我的前端代码: <script src="http://code.jquery.

我的代码有问题,就像一个按钮。它显示了喜欢的数量。若用户还并没有投票(cookie),他可以点击并计数器增加。问题是计数器并没有在第一次点击时更新(若我停用cookie检查并多次投票),在下一次刷新时一切都更新了。似乎在后端插入之前发生了一些计数。我假设probem在JavaScript中,ajax post跨域工作,但给出了错误,这就是为什么“error:setCookie和updateButton()

这是我的前端代码:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>


<div><a id="like_button" href="#">Like</a></div>
<script>
var url = "http://thumbs-up.some-server.com/";
var appName = "next_test";

document.write("<script src=\"" + url + "jquery.cookie.js\"><\/script>");
$(document).ready(function(){
  updateButton();
  $("#like_button").click(function(){
      if ($.cookie(appName + "_voted") == "true") {return;}
      $.ajax({
          type: "POST",
          dataType: "json",
          crossDomain: true,
          url: url + "increase_counter.php",
          data: {referrer: appName},
          success: setCookieAndUpdateButton(),
          error: setCookieAndUpdateButton()
      });
  });
});
function setCookieAndUpdateButton()
{
    updateButton();
    $.cookie(appName + "_voted", "true", {expires: 20*365});
}
function updateButton()
{
    $.ajax({
        type: "GET",
        async: false,
        contentType: "application/json",
        dataType: "jsonp",
        jsonpCallback: 'callback4jquery',
        url: url + "get_counter_for_referrer.php",
        data: {referrer: appName},
        success: function (json) {
          if ($.cookie(appName + "_voted") != "true"){
            $("#like_button").html("<a id=\"like_button\" href=\"#\"><img src=\"" + url + "like.png\">Good to know " + json.count + "x</a>")
          }
          else{
            $("#like_button").html("<span id=\"like_button\"><img src=\"" + url + "like.png\">Good to know "  + json.count + "x</span>");
            $('#like_button').unbind('click');
          }
        }
    });
}
</script>

变量url=”http://thumbs-up.some-server.com/";
var appName=“下一次测试”;
文件。填写(“”);
$(文档).ready(函数(){
updateButton();
$(“#喜欢_按钮”)。单击(函数(){
if($.cookie(appName+“_-voted”)==“true”){return;}
$.ajax({
类型:“POST”,
数据类型:“json”,
跨域:是的,
url:url+“increase_counter.php”,
数据:{referer:appName},
成功:setCookie和updateButton(),
错误:setCookieAndUpdateButton()
});
});
});
函数setCookieAndUpdateButton()
{
updateButton();
$.cookie(appName+“_-voted”,“true”,{expires:20*365});
}
函数updateButton()
{
$.ajax({
键入:“获取”,
async:false,
contentType:“应用程序/json”,
数据类型:“jsonp”,
JSONPCCallback:'callback4jquery',
url:url+“获取\u referer.php的\u计数器”,
数据:{referer:appName},
成功:函数(json){
if($.cookie(appName+“_投票”)!=“true”){
$(“#喜欢#按钮”).html(“”)
}
否则{
$(“#like_button”).html(“好消息”+json.count+“x”);
$('like#u按钮')。解除绑定('click');
}
}
});
}

在第一个ajax调用中,按如下方式更改代码:

 success: setCookieAndUpdateButton,
 error: setCookieAndUpdateButton

没有
()
,两者都是是的,我的同事告诉我,应该是函数名而不是函数调用。我只是通过实践来学习javascript,不知道太多细节:)