简体   繁体   English

在没有警告的情况下强制同步XMLHttpRequest / Ajax

[英]Force synchronous XMLHttpRequest / Ajax without warning

Ok so obviously synchronous has been deprecated because it is bad for the user experience. 好的,显然同步已被弃用,因为它对用户体验不利。

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. 主线程上的同步XMLHttpRequest因其对最终用户体验的不利影响而被弃用。 For more help http://xhr.spec.whatwg.org/ 如需更多帮助http://xhr.spec.whatwg.org/

And we all know that. 我们都知道。 But I still want to use it for something completely different than the user experience, on the development side. 但我仍然希望在开发方面将它用于与用户体验完全不同的东西。 I understand they don't want it used, but why remove the feature? 我知道他们不想使用它,但为什么删除该功能? These kind of things should be accesible via the "non-default" configuration via the options symbolizer. 这些东西应该可以通过选项符号化器通过“非默认”配置访问。 IMO it will be reversed one day. 国际海事组织有一天会被撤销。

Anyway, the question is: What kind of hack can I use to emulate synchronous Ajax or XMLHttpRequest, without receiving the warning message? 无论如何,问题是:我可以使用什么样的hack来模拟同步Ajax或XMLHttpRequest,而不会收到警告消息? My assumption is eventually it won't work in the browsers once they fully deprecate it. 我的假设是,一旦完全弃用它,它最终将无法在浏览器中运行。

Edit : This solution doesn't work. 编辑此解决方案不起作用。 I'll leave it here to discourage trying a similar method. 我会把它留在这里以阻止尝试类似的方法。 If someone can actually find a working solution, I'll remove this answer. 如果有人能够找到有效的解决方案,我会删除这个答案。

If you really want to fake a synchronous ajax request, you can use jQuery's .delay() function, and just keep checking if an asynchronous request is complete. 如果你真的想伪造同步ajax请求,可以使用jQuery的 .delay()函数,并继续检查异步请求是否完整。

function fakeSynchronous(ajaxSettings) {
  var promise = $.ajax(ajaxSettings);
  while (promise.state() === "pending") {
    $("html").delay(1000);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM