Ajax 中的 GET 与 POST [英] GET vs POST in Ajax

查看:30
本文介绍了Ajax 中的 GET 与 POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ajax 请求的 GETPOST 有什么区别?

What is the difference between GET and POST for Ajax requests?

我看不出这两者之间有什么区别,除了当我使用 GET 时,参数是在 URL 中发送的,这对我来说没有任何区别,因为所有请求都是在后台制作,用户没有发现任何区别.

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.

PUTDELETE 方法的用途是什么?

edit: What are PUT and DELETE methods used for?

推荐答案

GET 旨在从服务器获取数据.POST(以及鲜为人知的朋友 PUT 和 DELETE)专为修改服务器上的数据而设计.

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 请求不应导致从应用程序中删除数据.如果你有一个链接,你可以点击 GET 来删除数据,那么谷歌抓取你的网站就可以点击你所有的删除"链接.

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.

可以在此处找到规范答案,其中引用了HTML 2.0 规范:

The canonical answer can be found here, which quotes the HTML 2.0 spec:

如果表单的处理是幂等的(即它没有持久的对状态的可观察影响world),那么表单方法应该是得到.许多数据库搜索没有可见的副作用,使理想查询表单的应用.

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.

如果与表单处理相关的服务有副作用(例如,修改一个数据库或订阅服务),方法应该是POST.

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.

在您的 AJAX 调用中,您需要使用您的服务器支持的任何方法.您应该始终设计您的服务器,以便通过 POST/PUT/DELETE 调用修改数据的操作.其他评论有指向 REST 的链接,通常将 C/R/U/D 映射到POST 或 PUT"(创建)/GET(读取)/PUT(更新)/DELETE(删除).

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 中的 GET 与 POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆