4

ajax中断请求 - XMLHttpRequest.abort()方法终止请求

 2 years ago
source link: https://www.fly63.com/article/detial/11075
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
更新日期: 2022-01-26阅读量: 41标签: 请求分享

扫一扫分享

如果该请求已被发出,XMLHttpRequest.abort() 方法将终止该请求。当一个请求被终止,它的 readyState 将被置为 XMLHttpRequest.UNSENT (0),并且请求的 status 置为 0。

示例代码

var xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method, url, true);

xhr.send();
xhr.abort();

浏览器兼容性

IE浏览器从版本7开始使用了标准的 XMLHttpRequest 。

61f1069c1ade1.jpg

中断对后端的影响

前端调用XMLHttpRequest.abort(),会对后端的产生什么影响么?

http请求是典型的请求/响应模型。发送了就是发送了, 接受了就是接受了。算是浏览器单方面违约,请求或者是没发出来,或者是被浏览器拒收,但无论哪种都不是服务器能感知到的。

所以不要用abort方法来作为终止对服务器的请求操作,只能当做在前端页面立刻停止执行ajax成功后的方法,因为你执行abort方法后,ajax很可能已经对服务端发送了请求,只是还未返回回馈信息而已。

站长推荐

1.云服务推荐: 国内主流云服务商/虚拟空间,各类云产品的最新活动,优惠券领取。领取地址:阿里云腾讯云硅云

链接: https://www.fly63.com/article/detial/11075


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK