GET vs POST in AJAX?

2019-01-08 12:55发布

Why are there GET and POST requests in AJAX as it does not affect page URL anyway? What difference does it make by passing sensitive data over GET in AJAX as the data is not getting reflected to page URL?

标签: ajax http post get
9条回答
三岁会撩人
2楼-- · 2019-01-08 13:11

Thanks.. I mainly use the GET method with Ajax and I haven't got any problems until now except the following:

Internet Explorer (unlike Firefox and Google Chrome) cache GET calling if using the same GET values.

So, using some interval with Ajax GET can show the same results unless you change URL with irrelevant random number usage for each Ajax GET.

查看更多
孤傲高冷的网名
3楼-- · 2019-01-08 13:15

Another difference between GET and POST is the way caching is handled in browsers. POST response is never cached. GET may or may not be cached based on the caching rules specified in your response headers.

查看更多
不美不萌又怎样
4楼-- · 2019-01-08 13:21

Two primary reasons for having them:

  1. GET requests have some pretty restrictive limitations on size; POST are typically capable of containing much more information.

  2. The backend may be expecting GET or POST, depending on how it's designed. We need the flexibility of doing a GET if the backend expects one, or a POST if that's what it's expecting.

查看更多
干净又极端
5楼-- · 2019-01-08 13:21

It's simply down to respecting the rules of the http protocol.

Get - calls must be idempotent. This means that if you call it multiple times you will get the same result. It is not intended to change the underlying data. You might use this for a search box etc.

Post - calls are NOT idempotent. It is allowed to make a change to the underlying data, so might be used in a create method. If you call it multiple times you will create multiple entries.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-08 13:24

When we use the GET method in Ajax, only the content of the value of the field is sent, not the format in which the content is. For example, content in the text area is just added in the URL in case of the GET method (without a new line character). That is not the case in the POST method.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-08 13:25

You normally send parameters to the AJAX script, it returns data based on these parameters. It works just like a form that has method="get" or method="post". When using the GET method, the parameters are passed in the query string. When using POST method, the parameters are sent in the post body.

Generally, if your parameters have very few characters and do not contain sensitive information then you send them via GET method. Sensitive data (e.g. password) or long text (e.g. an 8000 character long bio of a person) are better sent via POST method.

查看更多
登录 后发表回答