0

I am trying to make a request from jQuery to see if there is enough resources in the storage to build a house. I do not really understand the difference between the ajax-functions $.get, $.post and $.ajax, and when to use which. I think that $.ajax is a more advanced function which also includes get and post, but when do I use get, and when do I use post? And also, do I use .get in the right way here?

Here is my jQuery code:

    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();
        }
    });

And the 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;
}
?>
rablentain
  • 6,641
  • 13
  • 50
  • 91

2 Answers2

1

You have to learn what HTTP methods are. Here are some good references:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html http://www.w3schools.com/tags/ref_httpmethods.asp

Basically, you have to use GET to get data from the server while using POST to give data to the server. However, there is no real restriction on which method you should use. It depends on using scenario, and it's worth noting that they both have their own limitation.

The backward of POST is when your users want to share filtered result with others, they can not just copy&paste the link, which is disappointing. The backward of GET is the server may lost information if the url is too long(more info).

One more thing, some guys would misunderstand that POST is safer than GET as user can not see the sent data. However, unless you use SSL, they are both unsafe for intentional guy.

In your case, your target is "creating a house in database". Although you have to check the resources before building it, which seems like "getting info from the server", your final goal is storing. Therefore, in my opinion, using POST is more logical.

Community
  • 1
  • 1
Brian
  • 30,156
  • 15
  • 86
  • 87
  • So if I want to get an answer from the server based on my question, in this case three parameters (house, x and y) is that a "get data"-thing or a "give data"-thing? I think it is both... – rablentain Sep 23 '13 at 08:20
  • you have to use `GET`. you are getting data from the server with some filters(house, x and y). the filters are not the data that are going to be stored on the server. Use `POST` when you are sending data that would be stored on the server. – Brian Sep 23 '13 at 08:24
  • Ok! I read the w3schools-link but did not understand what the bad thing is about POST? Why not always use it? Is it just if I want the possibility to save a link to the request? And also, I have not mentioned it above but the PHP-script also stores the just build house in database, is GET still right? – rablentain Sep 23 '13 at 08:29
  • sorry, I misunderstood you question. I have updated the answer. – Brian Sep 23 '13 at 09:05
  • @theva, if this answer solved your problem, please remember to accept it. Therefore, people who have the similar problem can be helped. – Brian Oct 05 '13 at 13:55
0

$.get and $.post functions are just a Shorthand Methods to GET and POST requests with $.ajax method, you can use $.get to make GET request and $.post to make POST request with standard options like: url, data, success callback and data type.

$.get( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

$.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

And use $.ajax method to make requests with more options.

Basically you can rewrite your example to:

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();
    }
});
Community
  • 1
  • 1
Timur K.
  • 36
  • 3
  • Ok, thanks! How do I answer with PHP in the best way? It seems like there would be a better solution than just echoing out 1 or 0. Is it? – rablentain Sep 23 '13 at 08:45
  • there is no restriction on what the server is returning, so it can be JSON, XML or even plane text. But the easiest response type is JSON, as it can be easily handled on client side. Also jQuery can automatically recognise the response type and return the proper JSON object to your success callback, or you can specify it manually in dataType option. Check out this [http://php.net/manual/en/book.json.php](http://php.net/manual/en/book.json.php) for more info. – Timur K. Sep 25 '13 at 02:42