3

Using

$.getJSON();

or

 $.post();

I'm trying to send some parameters through a page that is just for AJAX request and get some results in JSON or html snippet.

What I want to know is that which one is faster?

Assume the HTML file would be simply plain boolean text (true or false)

sohel khalifa
  • 5,528
  • 3
  • 32
  • 47
jwchang
  • 9,797
  • 15
  • 53
  • 86

7 Answers7

6

As others said there is no real difference between the two functions, because both of them will be sent by XMLHttpRequest.

If the server is handling both of the requests with the same code then the handling times should be the same.

Therefore the question can be translated to which one is faster the HTTP GET request or the POST request?

Because the POST request needs two additional HTTP headers (Content-Type and Content-Length) comparing to the GET request the latter should be faster (because less data will be transferred).

But that's just the speed, I think it's better to follow the REST guidelines here. Use POST if you're modifying something, use GET if you want to fetch something.

And one another important thing, GET responses could be cached, but I was having problems caching POST ones.

Community
  • 1
  • 1
KARASZI István
  • 28,974
  • 8
  • 95
  • 116
3

i dont think it will make a difference both make use of ajax, .post loads the data using http post request where as getJSON uses a http get request more over you dont have to explicitly tell getJSON the dataType

Rafay
  • 30,236
  • 5
  • 64
  • 98
3

If it is a HTTP action that is retrieving data from the server without persisting (updating) anything, GET is the correct semantic to use.

Both post and get use HTTP so performance difference will be negligible, especially considering the variables of WAN communication.

Xhalent
  • 3,866
  • 20
  • 21
2

This is old but ...

We all have to remember about: CSRF/XSRF.

If you do it this way:

$.ajax({
    type: "POST", 
    dataType: "json",
    url: url, 
    data: {
    token : 'pass-some-security-token-here'
    },
    cache: false,
    success: function(data) {
    //do your stuff here
    }
});

you can receive it then like this, nullifying most CSRF/XSRF

if (isset($_POST['token'])) { //you can also test token further
    //do your stuff her and send back result
} else {
    //error: sorry, invalid, or no security token
}

In many cases GET is an invitation for bad guys, as getJSON uses GET HTTP request.

Jeffz
  • 1,957
  • 5
  • 30
  • 48
2

They are both wrappers/shorthand methods for jQuery.ajax, so there wont be a performance difference.

NimChimpsky
  • 43,542
  • 55
  • 186
  • 295
1

$.getJSON(); is a shortcut to $.ajax(); which also calls $.post(); so you won't see much difference (but it will be easier to use $.getJSON() directly).

See the jquery doc

[EDIT] NimChimpsky was faster than me...

JMax
  • 24,408
  • 12
  • 63
  • 87
0

There are no difference, Because both are using XMLHttpRequest.

First, $.getJSON() is a shorthand Ajax function, which is equivalent to:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

https://api.jquery.com/jQuery.getJSON/

Second, $.post() is also a shorthand Ajax function, which is equivalent to:

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

https://api.jquery.com/jquery.post/

PKPrabu
  • 331
  • 2
  • 17