POST与Ajax调用 [英] POST versus Ajax call

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

问题描述

现在,大部分现代浏览器支持AJAX和客户端的请求,而不进行自检,什么是POST(表单提交)?

Now that most modern browsers support AJAX and client-side requests without performing a POST, what is the role of POST (form post)?

有没有的情况下,或当POST将永远是pferred到XmlHtt prequest $ P $一般规则?

Are there situations or general rules when a POST will always be preferred to a XmlHttpRequest?

所有的POST正在做的是把可变键值对服务器的脑袋里。 POST的我能想到的优点是大量的数据和移动浏览器。是否有很多人我失踪?

All that POST is doing is placing variable key value pairs inside the server head. The advantages of POST I can think of are large amounts of data and mobile browsers. Are there many others I'm missing?

N.B。我知道你可以使用AJAX调用执行的帖子,我说的主要是关于使用<形式GT; 标记

N.B. I know you can perform POSTs with AJAX calls, I'm talking primarily about with a <form> tag

推荐答案

这问题有点棘手,因为它合并了两个概念。首先是张贴和第二个是阿贾克斯。张贴,相比入门,是一个不同的HTTP方法有不同的实现和语义。阿贾克斯,或XmlHtt prequest,有其正常的请求/导航对手。可以使用POST或GET为XmlHtt prequest和正常航行和表单提交(当然,POST始终是一个表单提交)。之间XmlHtt prequest和正常请求不同的是,一个普通的请求替换所述页与新页。

This question is a little tricky because it conflates two concepts. The first is POSTing and the second is Ajax. POSTing, as compared to GETting, is a different HTTP method with different implementation and semantics. Ajax, or XmlHttpRequest, has its counterpart in normal requesting/navigation. You can use POST or GET for both XmlHttpRequest and normal navigation and form submission (well, POST is always a "form submission"). The difference between XmlHttpRequest and normal requests is that a normal request replaces the page with a new page.

您可以写几乎只使用Ajax来获取数据,并改变DOM任何网站;这是主要的Gmail是如何工作的。有传统意义上没有表单提交。但仍有GET和POST,因为服务器和浏览器间preT的结果不同。 GET应该是幂等; POST是用于那些更改服务器的状态操作。例如,一个ECOM事务应该是一个POST。这不使用Ajax的时候,因为你要代理服务器也明白这是一个POST和改变,他们不应该尝试缓存响应。

You could write just about any website using only Ajax to get data and change the DOM; that's mainly how Gmail works. There are no "form submissions" in the traditional sense. But there are still GETs and POSTs because the server and browsers interpret the results differently. GET is supposed to be idempotent; POST is meant for operations that change the state on the server. For example, an ecom transaction should be a POST. This doesn't change when using Ajax because you want proxy servers to also understand that this is a POST and that they shouldn't try to cache the response.

也有优点和缺点使用GET VS POST。你不能书签一个POST的结果,因为参数是隐藏的。你不能得到的东西与无限长度的参数值,因为IE浏览器只支持约2000个字符。

There are also advantages and disadvantages to using GET vs POST. You can't bookmark the results of a POST because the parameters are hidden. You can't GET something with parameter values of unlimited length because IE only supports about 2000 chars.

也有一些缺点使用Ajax与正常提交;你不能书签结果页面;如预期的后退按钮将无法正常工作。使用Ajax你不能上传文件。但是,使用Ajax,你可以最大限度地减少传输的数据,并提供方便的更新页面(如监测的一个漫长的过程的状态),没有恼人的闪烁或刷新。

Also there are disadvantages to using Ajax vs normal submissions; you can't bookmark the resulting page; the back button won't work as expected. You can't upload a file using Ajax. But with Ajax you could minimize the data transfered and also provide convenient updates to a page (such as monitoring the status of a long process) without annoying flickering or refreshing.

在总结这两个请求类型,Ajax和传统的表单提交,都可以用GET和POST使用,并且有优点和缺点每个。以上两种类型可以做一切其他的都可以,所以你可以期望看到一个组合为可预见的将来。

In summary the two request types, Ajax and traditional form submission, can both be used with GETs and POSTs, and there are pros and cons for each. Neither type can do everything the other can and so you can expect to see a mix for the forseeable future.

这篇关于POST与Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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