我试图从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 –