qwertymk
qwertymk

Reputation: 35274

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.

Upvotes: 9

Views: 6110

Answers (5)

jAndy
jAndy

Reputation: 236022

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.

Upvotes: 12

diggersworld
diggersworld

Reputation: 13080

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/

Upvotes: 1

Dunhamzzz
Dunhamzzz

Reputation: 14798

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.

Upvotes: 2

Josh
Josh

Reputation: 12566

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.

Upvotes: 1

Jakub Konecki
Jakub Konecki

Reputation: 46008

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

Upvotes: 0

Related Questions