【问题标题】:Haxe JS Http.requestUrl fails with InvalidAccessErrorHaxe JS Http.requestUrl 因 InvalidAccessError 而失败
【发布时间】:2021-12-03 14:29:48
【问题描述】:

在 haxe/js 中,同步 http 请求失败。比如这个

haxe.Http.requestUrl("http://www.anyurl.com");

在 Firefox 上导致此错误:

InvalidAccessError: XMLHttpRequest.responseType setter: synchronous XMLHttpRequests do not support timeout and responseType

看起来 haxe/js http 实现设置了 responseType here

有解决办法吗?

【问题讨论】:

    标签: haxe


    【解决方案1】:

    您可以在您的项目中添加 HttpJs 的副本并根据需要对其进行调整,但是 - 正如人们已经指出的那样 - 同步请求会在持续时间内冻结整个网页并且已被弃用 年 因为这个。

    【讨论】:

      【解决方案2】:

      https://api.haxe.org/haxe/http/HttpBase.html

      这个类可以用来处理 Http 请求 平台。有两种预期用途:

      • 调用 haxe.Http.requestUrl(url) 并以字符串形式接收结果(仅在 sys 目标上可用)
      • 创建一个新的 haxe.Http(url),注册 onData、onError 和 onStatus 的回调,然后调用 request()。

      示例:https://try.haxe.org/#1E28AD28

      【讨论】:

      • 第一个选项(我的例子)在 JS 中不起作用。第二个选项(您的示例)进行异步调用。我在问同步。所以听起来不支持同步http调用。但后来我不明白为什么Httpjs.requestUrl 存在。
      • 你给出的例子可以通过添加http.async = false;来同步,但是它会给出同样的错误。
      • 第一个选项仅在sys目标上可用
      • 我看到了,但是在标准库中存在一个特定于 js 的同步 http 调用实现似乎是一个明显的矛盾。我猜该方法仅适用于旧浏览器或节点或其他东西。听起来好像没有解决方法。 Http 调用必须是异步的。
      • 是的,由于浏览器的行为,它们看起来应该是异步的 developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/… Note: Starting with Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27), Blink 39.0, and Edge 13, synchronous requests on the main thread have been deprecated due to their negative impact on the user experience. 为什么需要同步调用?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2012-10-03
      • 2014-07-16
      • 2021-11-23
      • 2018-10-04
      • 2020-03-23
      • 2014-05-12
      相关资源
      最近更新 更多