简体   繁体   中英

jquery $.post() vs $.get()

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.

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.

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.

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 .

如果你想在浏览器中显示页面(你想刷新页面的一部分$.get() ,可以使用$.get()因为你不需要发布任何数据,或$.load() )。

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM