在没有表单的情况下将POST数据发送到AJAX

时间:2014-01-25 08:27:52

标签: php jquery ajax

我有一个PHP脚本,由链接点击触发的AJAX调用调用。通常,我只是使用一个表单并将所有表单数据传递给我的PHP脚本,但我想通过我的AJAX手动发送POST数据,这样我就可以避免使用表单了。我怎么能这样做?

我的JQuery / AJAX:

$(".likes-count a").click(function() {

    var post= $(this).parents(".post-bottom").find('.comment-input').val();
    // this value is from another form, this is the value that I need in my PHP script

    // grabs and updates the comment count
    $.ajax({  
        type: "POST",  
        url: "myscript.php",  
        data: 'post-id' : post, // This is what I have tried so far
        success: function(html) {
            alert(html);
        }
    });

});

3 个答案:

答案 0 :(得分:3)

我认为你正在寻找这个:

$.ajax({  
    type: "POST",  
    url: "myscript.php",  
    data: {'post-id':post},  //notice curly braces
    success: function(html) {
        alert(html);
    }
});

如果您想发送多个参数,可以用逗号分隔发送它们:

data: {'post-id':post,'param2':'param2Value'},

答案 1 :(得分:0)

试试这个:

$.ajax({  
   type: "POST",  
   url: "myscript.php",  
   data: {post-id:post,article:nextvalue},
   success: function(html) {
      alert(html);
   }
});

答案 2 :(得分:0)

我认为你的代码很好,但改变了这个

data: 'post-id' : post

到这个

data: {'post-id':post}

就是这样......

相关问题