我試圖從jQuery做出請求,看是否有足夠的資源在存儲中建造房屋。我真的不明白ajax函數$ .get,$ .post和$ .ajax之間的區別,以及何時使用哪個。 我認爲$ .ajax是一個更高級的函數,它也包含get和post,但是什麼時候使用get,以及什麼時候使用post?而且,我在這裏以正確的方式使用.get?最佳做法ajax,簡單的變量請求
這裏是我的jQuery代碼:
var x = 10 // x-position
var y = 10 // y-position
$.get('request.php?house=cottage&x='+x+'&y='+y, function(data){
if(data == 1){ // If there is enough resources etc... return 1.
itemId++; // Set unique id for this building.
$('body').append("<div class='house' id='" + itemId + "'></div>");
$('#'+itemId).css({
marginLeft: x - ($('.house').width())/2,
marginTop: y - ($('.house').width())/2
});
$('#rightMouseMenu').hide();
}
});
而且request.php:
<?php
$house = $_GET['house'];
$x = $_GET['x'];
$x = $_GET['y'];
// Some request to database to see if there is enough resources to build a house in enoughResources()
if(enoughResources() == 1){
echo 1;
}else{
echo 0;
}
?>
http://stackoverflow.com/questions/1344303/jquery-ajax-vs-get-post –