2014-04-14 20 views
0

我正在做一個遊戲,我需要通過發佈請求發送一些變量到排行榜頁面。如何添加更多變量到這個AJAX帖子...?

這是我目前郵編:

var toDatabase = confirm('Do you want to save your score to the database?'); 
if (toDatabase) { 
    $.post("leaderboards.php", {"Level1": "Level1"}, function() { 
    window.location.href="leaderboards.php"; 
}); 
} else { 
    $.post("gameL2.php", {"Level1": "Level1"}, function() { 
     window.location.href='gameL2.php'; 
    }); 
} 

這上面的代碼鏈接到leaderboards.php文件是一個我希望增加更多的變數太多。如何才能做到這一點?我要添加一對新的大括號嗎?

+0

這裏的例子可能會有幫助:https://api.jquery.com/jQuery.ajax/#entry-examples – showdev

回答

1

的第二個參數是一個簡單的目標:

$.post("leaderboards.php", {"Level1": "Level1", "abc": "def", "num": 123}, function() { 

只是逗號

4

數據通過一個對象發送,只需添加另一個關鍵/ VAL:

$.post("leaderboards.php", {"Level1": "Level1", "anotherKey": "anotherVal"}, function() { 
+3

''anotherKey「:」anotherVal「:) :) –

+0

謝謝,我正在尋找:)(將允許我接受)​​。 – user3481788

+1

@JustinC - 漫長的一天:\編輯並感謝! – tymeJV

1

您只會增加更多的變數您的數據對象。

if (toDatabase) { 
    $.post("leaderboards.php", {"Level1": "Level1","variable1":0,"variable2":"value","variable3":546}, function() { 
    window.location.href="leaderboards.php"; 
}); 

依此類推。

0

分開如果你有你想發送一個基本對象,並且只得到兩個不同的事件發生增加了兩個到那麼你可以使用jQuery.extend

var baseObj = { 
    Level1: "Level1" 
} 

if (toDatabase) { 
    $.extend(baseObj, { complete: true }); 
}else{ 
    $.extend(baseObj, {doSomething: "yeah" }); 
} 

這需要的只是添加另一個關鍵的想法/值對遠一點,更靈活