Ajax 中的 GET 与 POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/715335/
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 Jakub Arnold
What is the difference between GETand POSTfor Ajaxrequests?
是什么区别GET和POST的Ajax的请求?
I don't see any difference between those two, except that when I use GET, the parameters are send in URL, which for me don't really make any difference, since all requests are made on background and user doesn't find any difference.
我看不出这两者之间有什么区别,除了当我使用GET 时,参数是在 URL 中发送的,这对我来说没有任何区别,因为所有请求都是在后台发出的,而用户没有找到任何区别。
edit: What are PUTand DELETEmethods used for?
编辑:PUT和DELETE方法用于什么?
回答by crb
GET is designed for getting data from the server. POST (and lesser-known friends PUT and DELETE) are designed for modifying data on the server.
GET 旨在从服务器获取数据。POST(以及鲜为人知的朋友 PUT 和 DELETE)旨在修改服务器上的数据。
A GET request should never cause data to be removed from an application. If you have a link you can click on with a GET to remove data, then Google spidering your site could click on all your "Delete" links.
GET 请求不应导致从应用程序中删除数据。如果你有一个链接,你可以点击 GET 来删除数据,那么谷歌抓取你的网站就可以点击你所有的“删除”链接。
The canonical answer can be found here, which quotes the HTML 2.0 spec:
可以在此处找到规范的答案,其中引用了 HTML 2.0 规范:
If the processing of a form is idempotent (i.e. it has no lasting observable effect on the state of the world), then the form method should be GET. Many database searches have no visible side-effects and make ideal applications of query forms.
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.
如果表单的处理是幂等的(即它对世界状态没有持久的可观察影响),那么表单方法应该是 GET。许多数据库搜索没有明显的副作用,是查询表单的理想应用。
如果与表单处理相关的服务有副作用(例如,修改数据库或订阅服务),则该方法应为 POST。
In your AJAX call, you need to use whatever method your server supports. You should always design your server so that operations that modify data are called by POST/PUT/DELETE. Other comments have links to REST, which generally maps C/R/U/D to "POST or PUT"(Create)/GET(Read)/PUT(Update)/DELETE(Delete).
在您的 AJAX 调用中,您需要使用您的服务器支持的任何方法。您应该始终设计您的服务器,以便通过 POST/PUT/DELETE 调用修改数据的操作。其他评论有指向 REST 的链接,通常将 C/R/U/D 映射到“POST 或 PUT”(创建)/GET(读取)/PUT(更新)/DELETE(删除)。
回答by Ryan Smith
If you're sending large amounts of data, or sensitive data over HTTPS, you will want to use POST. If it's just a simple parameter, I would use GET.
如果您要通过 HTTPS 发送大量数据或敏感数据,您将需要使用 POST。如果它只是一个简单的参数,我会使用 GET。
GET requests have a limit to the amount of data that can be sent. I forget the exact number, but this can cause issues if you're sending anything substantial.
GET 请求对可以发送的数据量有限制。我忘记了确切的数字,但是如果您要发送任何实质性的东西,这可能会导致问题。
Basically the difference between GET and POST is that in a GET request, the parameters are passed in the URL where as in a POST, the parameters are included in the message body.
GET 和 POST 之间的基本区别在于,在 GET 请求中,参数在 URL 中传递,而在 POST 中,参数包含在消息正文中。
回答by Brian
回答by Sergey
GET requests are easier to exploit in CSRF (cross site request forgery) attacks. Namely fake POST requests require Javascript to be enabled on the user side, while fake GET requests are still possible just with img, script tags.
GET 请求在 CSRF(跨站请求伪造)攻击中更容易被利用。即假 POST 请求需要在用户端启用 Javascript,而假 GET 请求仍然可以仅使用 img、script 标签。
回答by Subroto Biswas
Both are used to send some data and receive some response using that data.
两者都用于发送一些数据并使用该数据接收一些响应。
GET: Get information store in server. Ie. Search, tweet, Person Information. If you want to send information then get request send request using process.php?name=subroto So it basically send information through url. Url cannot handle more than 2083 char. So for blog post can you remember it is not possible?
GET:获取服务器中的信息存储。IE。搜索、推特、个人信息。如果你想发送信息,然后使用 process.php?name=subroto get request 发送请求 所以它基本上是通过 url 发送信息。Url 不能处理超过 2083 个字符。所以对于博客文章,你记得这是不可能的吗?
POST: Post do same thing as get. User registration, User login, Big data send, Blog Post. If you need to send secure information then use post or for big data as it not go through url.
POST:Post 和 get 做同样的事情。用户注册、用户登录、大数据发送、博客文章。如果您需要发送安全信息,请使用 post 或大数据,因为它不通过 url。
AJAX: $.get() and $.post() contain features that are subsets of $.ajax(). It has much configuration.
AJAX:$.get() 和 $.post() 包含作为 $.ajax() 子集的功能。它有很多配置。
$.get () method, which is a kind of shorthand for $.Ajax (). When using $.get (), instead of passing in an object, you pass in arguments. At minimum, you'll need the first two arguments, which are the URL of the file you want to retrieve (i.e. ‘test.txt') and a success callback.
$.get() 方法,是$.Ajax() 的一种简写。使用 $.get() 时,不是传入对象,而是传入参数。至少,您需要前两个参数,它们是要检索的文件的 URL(即“test.txt”)和成功回调。
Summary:
概括:
$.get( url [, data ] [, success ] [, dataType ] )
$.post( url [, data ] [, success ] [, dataType ] ) // for sending secure or Large information
$.ajax( url [, settings ] ) // More Configaration
回答by Don Werve
Many web servers limit the length of the data that can be passed as part of the URL, so the GET request may break in odd ways that are hard to debug.
许多 Web 服务器限制可以作为 URL 的一部分传递的数据的长度,因此 GET 请求可能会以难以调试的奇怪方式中断。
Also, most server software logs URLs in the access logs, so if you pass sensitive information (such as passwords) in a GET request, this will in all likelihood be written to disk in plaintext.
此外,大多数服务器软件都会在访问日志中记录 URL,因此如果您在 GET 请求中传递敏感信息(例如密码),这很可能会以明文形式写入磁盘。
From a REST perspective, GET requests should have no side-effects -- they shouldn't modify data. So, if you're just GETting a resource by ID, this makes sense, but if you're committing changes to a resource, you should be using PUT, POST, or UPDATE for the http verb.
从 REST 的角度来看,GET 请求应该没有副作用——它们不应该修改数据。因此,如果您只是通过 ID 获取资源,这是有道理的,但如果您提交对资源的更改,则应该对 http 动词使用 PUT、POST 或 UPDATE。
回答by user86830
About me, i prefer POST. I reserve get to the events i know the sent value is limited to data i have the "control", for example, to retreive an item with an id. Example, "getitem?id=123", "deleteImtem?id=123", ... For the other cases, when i have a form fillable by a user, i prefer POST.
关于我,我更喜欢 POST。我保留访问我知道发送的值仅限于我拥有“控制权”的数据的事件,例如,检索带有 id 的项目。例如,“getitem?id=123”、“deleteImtem?id=123”,...对于其他情况,当我有用户可填写的表单时,我更喜欢 POST。
Like Ryan Smith have said, it's better to use POST to send a large amount of data, and less wories in cases of the use in others language/special chars (generally all majors javascript framework should'nt have any problems to deal with that but i think is less wories to use POST).
正如 Ryan Smith 所说,最好使用 POST 发送大量数据,并且在使用其他语言/特殊字符的情况下更少担心(通常所有专业的 javascript 框架都应该没有任何问题来处理,但是我认为使用 POST 不太担心)。
For the REST perspective, in my opinion, you can use this with a new project (to keep a consistency with the entire project).
对于 REST 的观点,在我看来,您可以将其用于新项目(以保持与整个项目的一致性)。
Finally, maybee some programs used in a network (URL loguers (ie.: to see if the employees lost their time on non-autorised sites, ...) proxys, ... ) or any other kind of tool can intercept the query. Somes will show in the reports the params you have sent with GET, considering it like a different web page. But in this situation, is could be not your problem it's changes from a project to an other! ;)
最后,也许是网络中使用的一些程序(URL 记录器(即:查看员工是否在非自动站点上浪费时间,...)代理,...)或任何其他类型的工具可以拦截查询. 有些会在报告中显示您使用 GET 发送的参数,将其视为不同的网页。但是在这种情况下,这可能不是您的问题,而是从一个项目更改为另一个!;)
回答by trante
First, general information. Use GETif you only read data, use POSTif you change something on database, txt files etc.
首先是一般信息。使用GET,如果你只读取数据,使用POST,如果你想改变什么数据库,txt文件等。
But the problem is, some browsers cache GETresults. I had problems with AJAXrequests in IE7, but at last I found out that browser caches GETresults. I rethought the flow and changes my request to POST.
但问题是,某些浏览器会缓存GET结果。我AJAX在 IE7 中遇到请求问题,但最后我发现浏览器缓存了GET结果。我重新考虑了流程并将我的请求更改为POST.
So, don't use GETif you don't want caching.
因此,GET如果您不想缓存,请不要使用。
(Of course you can disable caching in GET operations. But I didn't prefer it)
(当然你可以在 GET 操作中禁用缓存。但我不喜欢它)
回答by Hank Gay
回答by Click Upvote
If you are passing on any arguments with characters that can get messed up in the URL (such as spaces), you use POST. Otherwise you can use GET.
如果您要传递任何带有可能在 URL 中弄乱的字符的参数(例如空格),请使用 POST。否则,您可以使用 GET。
Generally, if you're just passing on a few tiny arguments you would use GET. But for passing on user submitted information such as blog entries, text, etc, its a good practice to use POST.
一般来说,如果你只是传递一些小参数,你会使用 GET。但是为了传递用户提交的信息,如博客条目、文本等,使用 POST 是一个很好的做法。
There are also certain frameworks that rely completely on segment based urls (such as site.com/products/133rather than site.com/products.php?id=333and these frameworks unset the GET variables for security. In such cases you would use POST allt the time.
还有一些框架完全依赖于基于段的 url(例如site.com/products/133而不是site.com/products.php?id=333这些框架为了安全而取消设置 GET 变量。在这种情况下,您将一直使用 POST。

