不建议在主线程上使用同步XMLHttpRequest。在打开新页面之前可以发送请求吗?

编程入门 行业动态 更新时间:2024-10-21 19:38:38
本文介绍了不建议在主线程上使用同步XMLHttpRequest。在打开新页面之前可以发送请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

由于不赞成使用同步XMLHttpRequest,我想知道是否存在某种替代方法来模拟类似的行为,因为非阻塞异步XMLHttpRequest在某些情况下不适合。

as the synchronous XMLHttpRequest is being deprecated, I'm wondering if there is some kind of alternative to simulate similar behaviour as non-blocking asynchronous XMLHttpRequests are not suitable for some cases.

我特别在谈论一种情况,当用户单击超链接时,我需要向服务器发送发布请求。在这里,不能使用PreventDefault。借助Synchronous XMLHttpRequest,我可以使用OnClick事件,发送请求,完成后浏览器将继续使用默认行为。使请求异步,将产生随机结果,因为一切都取决于新页面的加载速度,因此请求是否可以完成。

I'm talking in particular about a case where I need to send a Post Request to the server, when an user clicks a hyperlink. Using PreventDefault is not an option here. With Synchronous XMLHttpRequest I can use an OnClick event, send the Request, and once it's done the browser continues it's default behaviour. Having the Request asynchonous, would have random results, as everything depends on how quick the new page loads so the Request can finish or not.

是否有任何现代方式如何冻结浏览器的默认行为,直到发帖请求结束?

Is there any "modern" way how to freeze the browser's default behaviour till the Post Request finished?

问候和感谢。

推荐答案

在回调中:

window.location.href = "new url"

更多推荐

不建议在主线程上使用同步XMLHttpRequest。在打开新页面之前可以发送请求吗?

本文发布于:2023-11-24 23:04:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627249.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主线   建议   新页面   XMLHttpRequest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!