2014-01-25 49 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