HTML表单与XMLHTTPRequest? [英] HTML Form versus XMLHTTPRequest?

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

问题描述

使用HTML表单元素和XMLHTTPRequest进行POST后有什么区别?

What is the difference between POSTing with an HTML Form element and with an XMLHTTPRequest?

为什么表单允许重定向到另一个网页,但显然是XMLHTTPRequest不能?

Why can a form allow a redirect to another web page, but apparently a XMLHTTPRequest cannot?

如果我正在发出POST请求,我应该能够制定具有相同属性的请求并期望相同的行为。

If I'm making POST requests, I should be able to formulate a request with the same attributes and expect the same behavior.

推荐答案


使用HTML表单元素进行POST,使用XMLHTTPRequest进行
有什么区别?

What is the difference between POSTing with an HTML Form element and with an XMLHTTPRequest?

XMLHttpRequest允许执行异步操作,不会阻止客户端的UI。使用HTML表单时,客户端在执行操作时被阻止。

An XMLHttpRequest allows for asynchronous operations to be performed, which don't block the UI of the client. When using an HTML form, the client is blocked while the operation is being performed.

此外(正如您所指出的),HTML表单提交会导致页面重新加载到指定的URL,而XHR没有 - 它只是导致返回数据。返回的数据可能只是页面内容的一部分,因此XHR不会导致整页重新加载。它允许我们动态更新页面的一部分。

Also (as you point out), an HTML form submission causes a page reload to a specified URL, whereas an XHR does not - - it simply results in data being returned. The returned data may only be a portion of a page's content, so XHR does not cause a full page reload. It allows us to dynamically update a portion of a page.


为什么表单允许重定向到另一个网页,但显然是
XMLHTTPRequest不能?

Why can a form allow a redirect to another web page, but apparently a XMLHTTPRequest cannot?

XHR可以为其配置成功回调函数。操作成功完成后,回调函数可以执行任何操作,包括重定向。

An XHR can have a "success" callback function configured for it. When the operation is successfully completed, the callback function can do whatever you like, including a redirect.


如果我正在发出POST请求,我应该能够使用相同的属性来制定一个请求
并期望相同的行为。

If I'm making POST requests, I should be able to formulate a request with the same attributes and expect the same behavior.

你的意思是说HTML请求应该能够配置为与XHR请求相同吗?如果是这样,那么不,事实并非如此。可以将XHR请求配置为非常精细的级别(用户名和密码,数据类型,缓存,成功回调,故障回调等)。这(以及XHR的异步性质)是XHR受欢迎的原因。

Do you mean that an HTML request should be able to be configured the same as an XHR request? If so, then no, that's not the case. An XHR request can be configured to a very granular level (user name and password, data type, caching, success callback, failure callback, etc.). This (and the asynchronous nature of XHR) is the reason that XHR is popular.

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

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