2013-04-24 178 views
-1

對不起,如果這個問題沒有意義,我仍然試圖理解ajax,JavaScript和jQuery。所以這是我的問題。從javascript調用ajax函數

這是我的javascript代碼:

if (colorToCheck == gup("Player1")) { 
    document.getElementById('win').innerHTML=player1+" wins"; 
    //add ajax call to update my json file 
    redScore += 1 
} else if (colorToCheck == gup("Player2")) { 
    document.getElementById('win').innerHTML=player2+" wins"; 
    //add ajax call to update my json file 
    blackScore += 1 
} 

所以我想從這個塊調用AJAX。我搜查了網絡,但沒有得到我的答案。提前致謝。任何幫助高度讚賞。

+1

看看jQuery.ajax – neo 2013-04-24 18:29:07

+1

使用$ .ajax()會遇到什麼麻煩? – Ohgodwhy 2013-04-24 18:29:23

+0

你搜索了網絡,沒有得到答案?整個世界都在使用ajax和jquery。到處都有教程和例子。 – Timmerz 2013-04-24 18:30:18

回答

1
$.ajax({ 
    type: "POST", 
    url: "home.aspx", //you can call any function you want to execute in this url 
    data: datastring, 
    success: function(msg){ 
     alert("This is ajax call:" + msg); 
      // some code to exec on success 
    } 
}); 

這是一個樣本AJAX調用.. 如果你是專門爲JSON尋找然後再通過這個http://api.jquery.com/jQuery.getJSON/

+0

哦謝謝..我不知道我們可以做到這一點..再次感謝.. – user2234992 2013-04-24 18:35:54

1

使用這樣的事情:

$('#win').html(colorToCheck == gup("Player1") ? player1:player2 + " wins"); 

if (colorToCheck == gup("Player1")) 
    redScore += 1 
else if (colorToCheck == gup("Player2")) 
    blackScore += 1 

$.post("update-score.php", { player: x, score: y }); 

...

+0

謝謝你的回覆.. – user2234992 2013-04-24 18:37:58

1

看起來你在這裏有一個很好的答案。我只是想確保你知道在你的頁面頭文件中包含JQuery庫。如果您不想下載該庫,則可以使用Google託管版本的庫。

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 

更多的圖書館可在...

https://developers.google.com/speed/libraries/devguide#jquery

JQuery的教程

http://docs.jquery.com/Tutorials

AJAX簡介

http://www.w3schools.com/jquery/jquery_ajax_intro.asp

博客

http://net.tutsplus.com/tutorials/javascript-ajax/15-resources-to-get-you-started-with-jquery-from-scratch/

YouTube是學習的還有很多的好地方。

祝你好運!