向ajax请求jquery传递数据?

时间:2022-11-07 13:25:49

When i am trying to send data through ajax, its not passing data

当我试图通过ajax发送数据时,它并没有传递数据

$('#savenew').click(function () {
  var user = <?php echo $user?>;
  $.ajax({
    type: "POST",
    url: "actions/sub.php",
    data: user,
    success: function () {
      $('#savenew').html('<span>Unsubscribe</span>');
      $(this).removeAttr('id');
      $(this).attr('id', 'clean');
    }
  });
});

My PHP code on receiving end,

我的PHP代码在接收端,

if ($_POST['user']) {
 $user = $_POST['user'];
}

Am i doing something wrong ? Please help.

我做错什么了吗?请帮助。

1 个解决方案

#1


3  

This:

这样的:

data: user,

should be

应该是

data: {user: user},

Because you're looking for a POST variable called "user" and using its value. jQuery will accept an object literal and serialize it into POST data using the property names as keys and the property values as values. The nice thing about using an object (literal or otherwise) is that then jQuery handles doing the encoding of the values for you. You can use a string (data: "user=" + user), but then you have to worry about doing the encodeURIComponent part yourself for string parameters (no need for this numeric one).

因为您正在寻找一个名为“user”的POST变量并使用它的值。jQuery将接受一个对象文本并将其序列化为POST数据,使用属性名作为键,使用属性值作为值。使用对象(文字或其他)的好处是,jQuery可以为您处理值的编码。您可以使用一个字符串(数据:“user=”+ user),但是您必须为字符串参数(不需要这个数字)而担心使用encodeURIComponent。

You can also just do it all in one, with no user variable on the client side:

你也可以一次完成,客户端没有用户变量:

$('#savenew').click(function(){
    $.ajax({
        type: "POST",
        url: "actions/sub.php",
        data: {user: <?php echo $user?>},
        success: function(){
            $('#savenew').html('<span>Unsubscribe</span>');
            $(this).removeAttr('id');
            $(this).attr('id', 'clean');
        }
    });
});

...although having the user variable client-side is harmless, and of course if you want to use it in more than one place...

…尽管拥有用户变量客户端是无害的,当然,如果您想在多个地方使用它……

#1


3  

This:

这样的:

data: user,

should be

应该是

data: {user: user},

Because you're looking for a POST variable called "user" and using its value. jQuery will accept an object literal and serialize it into POST data using the property names as keys and the property values as values. The nice thing about using an object (literal or otherwise) is that then jQuery handles doing the encoding of the values for you. You can use a string (data: "user=" + user), but then you have to worry about doing the encodeURIComponent part yourself for string parameters (no need for this numeric one).

因为您正在寻找一个名为“user”的POST变量并使用它的值。jQuery将接受一个对象文本并将其序列化为POST数据,使用属性名作为键,使用属性值作为值。使用对象(文字或其他)的好处是,jQuery可以为您处理值的编码。您可以使用一个字符串(数据:“user=”+ user),但是您必须为字符串参数(不需要这个数字)而担心使用encodeURIComponent。

You can also just do it all in one, with no user variable on the client side:

你也可以一次完成,客户端没有用户变量:

$('#savenew').click(function(){
    $.ajax({
        type: "POST",
        url: "actions/sub.php",
        data: {user: <?php echo $user?>},
        success: function(){
            $('#savenew').html('<span>Unsubscribe</span>');
            $(this).removeAttr('id');
            $(this).attr('id', 'clean');
        }
    });
});

...although having the user variable client-side is harmless, and of course if you want to use it in more than one place...

…尽管拥有用户变量客户端是无害的,当然,如果您想在多个地方使用它……