Ajaxpost不发送数据

我用ajax和php构建了一个投票系统,我将数据发送到php页面,以便在db中保存数据。 我试图用ajax post和php发送数据。 我的问题是数据没有发送到页面。 我的js代码:

  $(document).ready(function(){ $.ajaxSetup({ url: 'vote.php', type: 'POST', cache: 'false' }); $('.vote').click(function(){ var self = $(this); var action = self.data('action'); var parent = self.parent().parent(); var imgid = ; if (!parent.hasClass('.disabled')) { if (action == 'up') { parent.find('#image-like').addClass('disabled_up'); $.ajax({data: {'imgid' : imgid, 'action' : 'up'}}); } else if (action == 'down'){ parent.find('#image-dislike').addClass('disabled_down'); $.ajax({data: {'imgid' : imgid, 'action' : 'down'}}); }; parent.addClass('.disabled'); }; }); });  

和我的HTML代码:

   

使用post方法。 这不是正确的代码,但它是一个想法,总是适合我。

 $('.vote').click(function(){ //Your vars var data='voteup'; //Your actions... ddClass/removeClass... $.post('vote.php',data,function(data){ //On your vote.php use "if($data=='voteup') else ;" //And show message here... alert(data); }); return false; }); 

vote.php的例子

  

这只是一个想法(:

您可以尝试更改此内容:

 if (!parent.hasClass('.disabled')) { 

对此:

 if (!parent.hasClass('disabled')) { 

一些说明:

来自文档 :

对于$.ajaxSetup()

描述:为将来的Ajax请求设置默认值。 不推荐使用它。

尝试使用.post()函数,您可以在操作完成后设置回调

 jQuery.post(URL_TO_REACH, {ID_VALUE1 : 'my value' , ID_VALUE2 : 'my second value' }) .done(function( data_ajax ) { // data_ajax : Your return value from your php script alert( data_ajax ); }) }); 

希望对你有帮助

官方文档: http : //api.jquery.com/jQuery.post/