不建议在主线程上使用同步XMLHttpRequest。在打开新页面之前可以发送请求吗? [英] Synchronous XMLHttpRequest on the main thread is deprecated. Any alternative to send request before opening new page?

查看:72
本文介绍了不建议在主线程上使用同步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。在打开新页面之前可以发送请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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