0

I am learning about AJAX using W3school resources, and there is a phrase in this URL that I do not understand: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp

It is about when POST should be used when using AJAX. It says:

GET or POST?

GET is simpler and faster than POST, and can be used in most cases.

However, always use POST requests when:

  • A cached file is not an option (update a file or database on the server).

  • Sending a large amount of data to the server (POST has no size limitations).

  • Sending user input (which can contain unknown characters), POST is more robust and secure than GET.

What does, A cached file is not an option (update a file or database on the server). mean?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
v8rs
  • 197
  • 4
  • 17
  • 1
    Maybe it can be rephrased as use POST when you need to update a file or database on the server. – Mikey Jun 16 '16 at 15:14
  • 1
    "A cached file is not an option" - It literally mean that AJAX GET caches (keeps) server response and returns it at the next request(s) without requesting the server. – Alex Kudryashev Jun 16 '16 at 15:15
  • 1
    Not an answer, but just some advice. Always use the HTTP verb most related to what you are doing. `GET` should always *get* data and *never* update the server. `POST` is generally used to *store* data/files on the server. See: https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods – gen_Eric Jun 16 '16 at 15:17
  • 1
    Does this help answer your question: http://stackoverflow.com/q/626057 (or http://www.w3schools.com/tags/ref_httpmethods.asp) – gen_Eric Jun 16 '16 at 15:17
  • check this http://stackoverflow.com/questions/715335/get-vs-post-in-ajax – Deep Jun 16 '16 at 15:20

1 Answers1

1

if a file is cached it is stored locally on the clients machine which means that the data is already present and there is no need to go get it from a server. The reason this isn't an option for the post (it is but not best practice) is because the posts job is to send data to the server with the intention of updating a record or a file (sometimes if it's a really small change to the configuration of the server you would store it in a .json or .config or .txt file. This could be a post to update that file) / or database record.

The post will hide the data being sent (kind of, you won't see it in the URL unlike a GET Request which will show the name=value pairs in the URL). Post request is meant to update a piece of data.

It's impossible to update the server data with the local cached data - because if you update the local file/data it's not updating on the server, which is accessed via RESTful CRUD (GET/GET:ID/POST/PUT/DELETE) (Create, Read, Update, Delete) patterns

Andrei
  • 1,196
  • 1
  • 11
  • 25
  • 1
    Then if I want to retrieve data from the server I should use GET. But as that data could already be in my cache I should allways pass a new parameter such as a random number or the current timestamp so that the latest data from the server is sent? – v8rs Jun 16 '16 at 15:32
  • exactly, but generally if it's user data or financial data (moving data) you would always want to request the data from the database not the local cache, - let's say it's financial data. How you do know there wasn't a withdraw in the last 10 seconds? you don't so you would do GET to the server. You would only want to cache static content - Ex(.html files, .css files, Images, buffer data for a video etc..) – Andrei Jun 16 '16 at 15:35
  • Then It could be said that this is the rule: To retrieve data-> Allways use GET. To be sure that that data is data from the DB (so it is the most recent data)-> Allways pass a new parameter ? – v8rs Jun 16 '16 at 15:44
  • you wouldn't need to pass a new parameter just calling the method ajax.get() <- again with all the correct parameters would return new data. so even if you do a get to localhost:3000/user/3000 <- each time you GET that page it would return fresh data from the database it could be the same data from earlier if nothing has been updated. – Andrei Jun 16 '16 at 15:46