AJAX 中的 GET 还是 POST?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1872965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GET vs POST in AJAX?
提问by Xinus
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 中有 GET 和 POST 请求,因为它无论如何都不会影响页面 URL?由于数据没有反映到页面 URL,因此通过在 AJAX 中通过 GET 传递敏感数据有什么区别?
回答by Daniel Vassallo
You should use the proper HTTP verb according to what you require from your web service.
您应该根据您对 Web 服务的要求使用正确的 HTTP 动词。
When dealing with a CollectionURI like: http://example.com/resources/
处理CollectionURI 时,例如:http://example.com/resources/
GET: List the members of the collection, complete with their member URIs for further navigation. For example, list all the cars for sale.
GET:列出集合的成员,并附上他们的成员 URI 以供进一步导航。例如,列出所有待售汽车。
PUT: Meaning defined as "replace the entire collection with another collection".
PUT:含义定义为“用另一个集合替换整个集合”。
POST: Create a new entry in the collection where the ID is assigned automatically by the collection. The ID created is usually included as part of the data returned by this operation.
POST:在集合中创建一个新条目,其中 ID 由集合自动分配。创建的 ID 通常包含在此操作返回的数据中。
DELETE: Meaning defined as "delete the entire collection".
DELETE:含义定义为“删除整个集合”。
When dealing with a MemberURI like: http://example.com/resources/7HOU57Y
在处理像这样的成员URI 时:http://example.com/resources/7HOU57Y
GET: Retrieve a representation of the addressed member of the collection expressed in an appropriate MIME type.
GET:检索以适当 MIME 类型表示的集合中寻址成员的表示。
PUT: Update the addressed member of the collection or create it with the specified ID.
PUT:更新集合的寻址成员或使用指定的 ID 创建它。
POST: Treats the addressed member as a collection in its own right and creates a new subordinate of it.
POST:将被寻址的成员视为一个集合,并为其创建一个新的下级。
DELETE: Delete the addressed member of the collection.
DELETE:删除集合的寻址成员。
Source: Wikipedia
资料来源:维基百科
回答by dxh
Well, as for GET, you still have the url length limitation. Other than that, it is quite conceivable that the server treats POST and GET requests differently; thus the need to be able to specify what request you're doing.
好吧,至于 GET,您仍然有 url 长度限制。除此之外,可以想象服务器以不同的方式处理 POST 和 GET 请求;因此需要能够指定您正在执行的请求。
回答by Chetan Sastry
Another difference between GETand POSTis the way caching is handled in browsers. POSTresponse is never cached. GETmay or may not be cached based on the caching rules specified in your response headers.
GET和之间的另一个区别POST是浏览器处理缓存的方式。POST响应永远不会被缓存。GET可能会或可能不会根据响应标头中指定的缓存规则进行缓存。
回答by DanH
It's simply down to respecting the rules of the http protocol.
这只是遵守 http 协议的规则。
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.
Get- 调用必须是幂等的。这意味着如果您多次调用它,您将获得相同的结果。它无意更改基础数据。您可以将其用于搜索框等。
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.
后调用不是幂等的。允许对底层数据进行更改,因此可以在 create 方法中使用。如果您多次调用它,您将创建多个条目。
回答by T.J. Crowder
Two primary reasons for having them:
拥有它们的两个主要原因:
GETrequests have some pretty restrictive limitations on size;POSTare typically capable of containing much more information.The backend may be expecting
GETorPOST, depending on how it's designed. We need the flexibility of doing aGETif the backend expects one, or aPOSTif that's what it's expecting.
GET请求对大小有一些非常严格的限制;POST通常能够包含更多的信息。后端可能会期待
GET或POST,这取决于它的设计方式。我们需要灵活地做 aGET如果后端期望一个,或者POST如果这是它所期望的。
回答by Salman A
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.
您通常将参数发送到 AJAX 脚本,它会根据这些参数返回数据。它的工作原理就像一个具有 method="get" 或 method="post" 的表单。使用 GET 方法时,参数在查询字符串中传递。使用 POST 方法时,参数在帖子正文中发送。
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.
通常,如果您的参数字符很少且不包含敏感信息,那么您可以通过 GET 方法发送它们。敏感数据(例如密码)或长文本(例如一个人的 8000 个字符长的生物)最好通过 POST 方法发送。
回答by Ecommerce website development
Thanks.. I mainly use the GET method with Ajax and I haven't got any problems until now except the following:
谢谢.. 我主要使用 Ajax 的 GET 方法,到目前为止我没有遇到任何问题,除了以下问题:
Internet Explorer (unlike Firefox and Google Chrome) cache GET calling if using the same GET values.
如果使用相同的 GET 值,Internet Explorer(与 Firefox 和 Google Chrome 不同)会缓存 GET 调用。
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.
因此,对 Ajax GET 使用一些间隔可以显示相同的结果,除非您更改 URL 并为每个 Ajax GET 使用不相关的随机数。
回答by ashish kumar
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.
我们在ajax中使用GET方法时,只发送字段值的内容,不发送内容的格式。例如,在 GET 方法的情况下,文本区域中的内容只是添加到 URL 中(没有换行符)。在 POST 方法中不是这种情况。
回答by ss ulrey
Others have covered the main points (context/idempotency, and size), but i'll add another: encryption. If you are using SSL and want to encrypt your input args, you need to use POST.
其他人已经涵盖了要点(上下文/幂等性和大小),但我将添加另一个:加密。如果您使用 SSL 并希望加密您的输入参数,则需要使用 POST。

