Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET vs. POST ajax requests: When and how to use either?

Tags:

What are the strengths of GET over POST and vice versa when creating an ajax request? How do I know which I should use at any given time? Is it a security-minded decision?

Also, what is the difference in how they are actually sent?

like image 576
johnnietheblack Avatar asked Sep 03 '09 22:09

johnnietheblack


People also ask

Should I use an HTTP GET or POST for my Ajax calls?

Use POST if your call is going to write any data at all to the server. In fact, you should not only use this criterion for selecting between GET and POST for your Ajax calls but also for when selecting which should be used for processing forms on your web page.

What is the difference between GET and POST in Ajax?

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

Should I use GET or POST?

Use GET if you want to read data without changing state, and use POST if you want to update state on the server.

What does Ajax GET request do?

What's AJAX? AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.


1 Answers

GETs should be used for idempotent operations, that is operations that can be safely repeated more than once without changing anything. Browsers will cache GET requests (for normal and AJAX requests)

POSTs should be generally be used for non-idenpotent operations, like saving something. Although you can use them for other operations if you want.

Data for GETs is sent over the URL query string. Data for POSTs is sent separately. Some browsers have a maximum URL length (I think Internet Explorer is 2048 characters), and if the query string becomes too long you'll get an error.

like image 59
jimr Avatar answered Sep 20 '22 23:09

jimr