最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery .getJSON vs .post which one is faster? - Stack Overflow

programmeradmin1浏览0评论

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)

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)

Share Improve this question edited Jul 15, 2013 at 12:39 sohel khalifa 5,5783 gold badges36 silver badges46 bronze badges asked Aug 23, 2011 at 11:46 jwchangjwchang 10.9k15 gold badges61 silver badges89 bronze badges
Add a comment  | 

7 Answers 7

Reset to default 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.

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

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.

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

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.

$.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...

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/

发布评论

评论列表(0)

  1. 暂无评论