2014-01-25 43 views
-2

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

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

}); 
+0

可能重复 - $ .ajax POST不会将.data发送到Web服务器](http://stackoverflow.com/questions/5596341/jquery-ajax-post-does-not-send-data-to-web-server) –

回答

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

在AJAX调用之后,不要忘记'返回false;'。 – Deryck

+0

@Deryck感谢您的提示。是的'返回false;'将需要,但在'ajax'之后,并在完成该功能之前。 – Bhushan

0

试试这个:

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

我觉得你的代码是不错,但改变这种

data: 'post-id' : post

这个

data: {'post-id':post}

就是这样...... [的jQuery