Get and post method in html

2019-08-09 05:29发布

问题:

what is the meaning of these three statements:

  1. If get method is used and if the page is refreshed it would not prompt before the request is submitted again.

  2. If the response of the page is always same for the posted query then use GET example database searches

  3. If the service associated with the processing of a form has side effects (for example, modification of a database or subscription to a service), the method should be POST.

The second one I understand partially: That the same result is generated when query is done from database using same keywords. For ex: "How to find ip address" will generate same results every time in a website until there are new posts that contain this string (How to find ip address). But why get is used I don't know.

回答1:

It would help to have the context where you found those guidelines, but:

If someone reloads a page which resulted from a POST operation, they'll usually get a confirmation dialog like "Do you want to resend the submitted data?", which is a useful warning for operations which have side-effects, such as creating records in a database. You don't want people to blindly create multiple copies of the same order, for instance.

If someone reloads a page which resulted from a GET operation, it will be refreshed without any further confirmation, on the assumption that there will be no potentially undesirable side-effects on the server.

There are also other considerations. You can create a direct link to a GET request, but not a POST, and POST data won't be stored in the browser history (though may be stored in other things like plugins or form-fillers).



回答2:

These statements appear to be giving guidance on when to use get vs post. The basic idea is that get is for read-only operations, and post is for update operations.



回答3:

GET is supposed to be idempotent - i.e. no matter how many times you repeat the operation, the result will be the same. The GET operation by itself does not cause a change in the service / data. e.g. load a thread on stack overflow. Doing get multiple times has no affect on the backend service.

POST on the other hand may cause a change in the backend data/service. e.g. post a new message to stack overflow. Doing the same post multiple times will cause the message to be posted multiple times.



回答4:

It says that if there is select operation of a database then perform GET operation as it will not change any data or modify in a database but if you use Deletion or Insertion this may change with GET which is not good as if Web Crawler also do some changes which is not good for any website.