简体   繁体   English

Ajax发布不发送数据

[英]Ajax post doesn't send data

I built a vote system with ajax and php, and I send data to php page for saved data in db. 我用ajax和php构建了一个表决系统,然后将数据发送到php页面以在db中保存数据。 I tried to send data with ajax post and php. 我试图用ajax post和php发送数据。 My problem is the data is not send to the page. 我的问题是数据没有发送到页面。 My js code: 我的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>

and my html code: 和我的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>

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;
});

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 (: 这只是一个主意(:

You can try changing this: 您可以尝试更改此设置:

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

to this: 对此:

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

Some Notes: 一些注意事项:

From the docs : 从文档

For $.ajaxSetup() $.ajaxSetup()

Description: Set default values for future Ajax requests. 描述:为将来的Ajax请求设置默认值。 Its use is not recommended. 不建议使用它。

Try using .post() function, you can set a callback when your action is done 尝试使用.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 );
                 })
              });

Hope this will help you 希望这个能对您有所帮助

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

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

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