[英]Ajax post doesn't send data
我用ajax和php構建了一個表決系統,然后將數據發送到php頁面以在db中保存數據。 我試圖用ajax post和php發送數據。 我的問題是數據沒有發送到頁面。 我的js代碼:
<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>
使用發布方法。 這不是正確的代碼,但這是一個主意,始終對我有用。
$('.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;
});
投票的例子
<?php
$data=$_POST['data'];
if($data=='voteup')
echo "You voted up!";
else echo "You voted down!";
?>
這只是一個主意(:
您可以嘗試更改此設置:
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/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.