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

javascript - jquery $.post() vs $.get() - Stack Overflow

programmeradmin4浏览0评论

I need to retrieve a simple page and use the data that it returns. Is there any difference between $.post() and $.get() should I be using one over the other?

I don't plan on submitting any data with the request.

I need to retrieve a simple page and use the data that it returns. Is there any difference between $.post() and $.get() should I be using one over the other?

I don't plan on submitting any data with the request.

Share Improve this question edited Mar 26, 2012 at 16:08 Dunhamzzz 14.8k4 gold badges53 silver badges74 bronze badges asked Mar 26, 2012 at 15:59 qwertymkqwertymk 35.3k30 gold badges124 silver badges184 bronze badges 1
  • Here's a quick run down of the most used jQuery AJAX methods: catchmyfame.com/2012/03/05/… – j08691 Commented Mar 26, 2012 at 16:06
Add a comment  | 

5 Answers 5

Reset to default 12

If you just want to retrieve the contents from an html document, use $.load() instead.

You can even retrieve partial information from that document by providing an additional selector:

$('#result').load('ajax/test.html');
$('#result').load('ajax/test.html #justThisContainerPlease');

see http://api.jquery.com/load/


To answer your question more generally, its no big difference whether you're using a POST or a GET request to a server, it depends on the amount of data you need to send. Typically, a GET request is limited to 2083 (because IE limits the query-string). So if you have a lot of data to send, you should use a POST request.

Technically, a GET request should be slightly faster. Because internally only one packet is sent instead of at least two (one for the header and one for the transmission body). But that really is high performance optimizing.

If you're not submitting data, then you actually should be using $.load();

$.get(); and $.post() are typically for submitting data to a server, so you don't need them in this context. There are big differences between POST and GET data, you should take some time to read up on them.

Here is a nice article explaining the differences between an HTTP POST and an HTTP GET. I myself prefer to use $.ajax(); and tweak it accordingly.

The main difference between them is that with POST you pass a collection of data and with GET you pass data in the URL. If you're passing a lot of data I'd suggest POST. If you're just calling a URL for a response then use get.

For a full understanding though checkout the jQuery documentation of each.

GET: http://api.jquery.com/jQuery.get/

POST: http://api.jquery.com/jQuery.post/

Go for $.get() as you don't need to post any data, or $.load() if you want to display the page in the browser (you want to refresh a part of the page).

发布评论

评论列表(0)

  1. 暂无评论