jquery - Ajax sends two files at one time through one onclick event in JavaScript -
jquery ajax seems sending 2 requests @ once when use onclick event javascript in tag. click once, , seems ok, when change id value invalid id value, sends 2 requests php file. think problem may caused browser caching javascript code. here's javascript code i'm using generate query:
function unlike_image(id, image_id, obj) { var url_unlike_image = base_url + 'profile/unlike_image'; $.ajax({ type: "post", data: { user_id: id, image_id: image_id }, url: url_unlike_image, success: function(data) { if (data.status=='error_exists') { alert('this image not exists'); } if (data.status=='success') { //like = - 1 view var str = $(obj).next().text(); var n = str.length; str_like = str.substring(1, n-1); var number_likes = parseint(str_like) - 1; $(obj).next().text('('+number_likes+')'); //change event click unlike $(obj).text('like'); $(obj).attr('onclick', 'like_image('+id+' ,'+image_id+ ',this); return false'); } } }); }
after changing true id wrong id, check website traffic, see 2 instances unlike_image called. first true id, , second wrong id.
Comments
Post a Comment