简体   繁体   English

AJAX成功功能不起作用

[英]AJAX success function not working

I made ajax function for button that if i click it means 'like' and record to database 我为按钮制作了ajax函数,如果我单击它就意味着“喜欢”并记录到数据库中
then the button is gonna change to 'dislike' and if click again it means delete record. 然后该按钮将变为“不喜欢”,如果再次单击,则意味着删除记录。

In the record part is working fine but i'm stuck on success function 在记录部分工作正常,但我被困在成功功能上
this is my whole javascript.. 这是我的整个JavaScript。

    <script type="text/javascript">
  function addLikes(userFRIEND,action) {
  $('.demo-tutor #tutorial-'+userFRIEND+' li').each(function(index) {
    $(this).addClass('selected');
    $('#tutorial-'+userFRIEND+' #rating').val((index+1));
    if(index == $('.demo-tutor #tutorial-'+userFRIEND+' li').index(obj)) {
      return false; 
    }
  });


  $.ajax({
  url: "add_follow.php",
  data:'userFRIEND='+userFRIEND+'&action='+action,
  type: "POST",
    beforeSend: function(){
    $('#tutorial-'+userFRIEND+' .btn-likes').html("<img src='http://www.seedsofpeace.org/gif/loading.gif' />");
  },
  success: function(data){
  var followings = parseInt($('#likes-'+userFRIEND).val());
  switch(action) {
    case "like":
    $('#tutorial-'+userFRIEND+' .btn-likes').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-danger btn-xs unlike" onClick="addLikes('+userFRIEND+',\'unlike\')">&nbsp;&nbsp;&nbsp;Unfollow&nbsp;&nbsp;&nbsp;</button>');
    followings = followings+1;
    break;
    case "unlike":
    $('#tutorial-'+userFRIEND+' .btn-likes').html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" class="btn btn-danger btn-xs like" onClick="addLikes('+userFRIEND+',\'like\')">&nbsp;&nbsp;&nbsp;Follow&nbsp;&nbsp;&nbsp;</button>')
    followings = followings-1;
    break;
  }
  $('#likes-'+userFRIEND).val(followings);
  if(followings>0) {
    $('#tutorial-'+userFRIEND+' .label-likes').html(followings);
  } else {
    $('#tutorial-'+userFRIEND+' .label-likes').html('0');
  }
  }
  });
}
    </script>

I try to put alert on success function and it's work. 我尝试对成功功能发出警报,它可以正常工作。 so I think the errors must be on 所以我认为错误一定在

$('#tutorial-'+userFRIEND+' .btn-likes').html("blabla");

#tutorial-'+userFRIEND+' .btn-likes #tutorial-'+ userFRIEND +'.btn-likes

but to be honestly i don't know how to fix it 但说实话我不知道如何解决

and this is the html that relate with javascript 这是与javascript相关的html
(I'll show via picture for easy looking.) (我将通过图片显示,以方便查找。)

HTML fig. HTML图

thank you so much for every suggestion. 非常感谢您的每一个建议。

seems you do wrong with the posted data, try do it in this format: 似乎您对发布的数据做错了,请尝试使用以下格式:

  url: "add_follow.php",
  data: {
    userFRIEND:userFRIEND, 
    action:action
  },
  type: "POST",

then try this selector: 然后尝试使用此选择器:

 console.log(userFRIEND) // this really logs "1" ? 
 $('#tutorial-'+userFRIEND+').find('.btn-likes').html("blabla");

Seems like you're using jquery without 似乎您正在使用没有

$(document).ready(function() {
    // your code
});

Be sure to put you js code at the end of the page and, as mentioned before me, put and object in ajax data property 确保将您的js代码放在页面的末尾,并像我之前提到的那样,将对象放在ajax数据属性中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM