Ajax帖子不发送数据 [英] Ajax post doesn't send data
本文介绍了Ajax帖子不发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用ajax和php构建了一个投票系统,我将数据发送到php页面,以便在db中保存数据。
我试图用ajax post和php发送数据。
我的问题是数据没有发送到页面。
我的js代码:
I built a vote system with ajax and php, and I send data to php page for saved data in db. I tried to send data with ajax post and php. My problem is the data is not send to the page. My js code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(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 = <?=$array['id'];?>;
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');
};
});
});
</script>
和我的HTML代码:
<a href="javascript:void(0);" id="image-like" data-action="up" class="vote"></a>
<a href="javascript:void(0);" id="image-dislike" data-action="down" class="vote"></a>
推荐答案
使用post方法。这不是正确的代码,但这是一个想法,总是适合我。
Use post method. This is not the correct code, but it's an idea, always works for me.
$('.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的例子
example of vote.php
<?php
$data=$_POST['data'];
if($data=='voteup')
echo "You voted up!";
else echo "You voted down!";
?>
这只是一个想法(:
It's just an idea (:
这篇关于Ajax帖子不发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文