Firefox似乎是唯一在执行此同步请求时不会出错的浏览器,为什么?

// Make sure to have your JS console open when you run this
var url = '//api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=83f67039ae0c3790030d256cb9029678';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, false); // SYNCHRONOUS
xhr.onload = alert.bind(null, 'Loaded');
xhr.send(null);


执行几乎相同的异步XMLHttpRequest不会导致错误,并且请求可以按预期完成:

// Make sure to have your JS console open when you run this
var url = '//api.soundcloud.com/resolve.json?url=http://soundcloud.com/matas/hobnotropic&client_id=83f67039ae0c3790030d256cb9029678';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true); // ASYNCHRONOUS
xhr.onload = alert.bind(null, 'Loaded');
xhr.send(null);


据我了解:
  • SoundCloud API返回正确的CORS header 。
  • SoundCloud resolve API执行302重定向到http://api.soundcloud.com/tracks/49931.json
  • 如果以异​​步方式执行请求,则它将成功遵循重定向并完成。
  • 如果该请求是通过同步执行来执行的,则它将在控制台中失败,并出现以下错误(无Firefox):
  • Chrome:“NetworkError:发生网络错误。”
  • Opera:“NetworkError:发生网络错误。”
  • Safari:“XMLHttpRequest异常101:同步请求中发生网络错误。”

  • 谁能解释为什么同步请求失败?以及为什么它会因奇怪的错误而失败,尤其是因为异步执行相同的请求时,它会失败?为什么该参数会有所不同?这是Firefox或WebKit / Blink人群的已知错误吗?

    编辑:我使用opened a new issue on Chrome's bug tracker,因为尚无人指出规范中可以正确解释此行为的任何内容。如果有机会,我可能会为WebKit打开一个类似的问题。

    最佳答案

    为了阻止使用同步请求,这似乎是一个故意的选择:

  • w3 Web应用程序邮件列表:Disable new response types for sync XHR in Window context
  • w3错误:Investigate if synchronous XHR in window context should not support new XHR responseTypes
  • Webkit:Synchronous XHR in window context should not support new XHR responseTypes for HTTP(S) requests
  • Mozilla:Investigate if synchronous XHR in window context should not support new XHR responseTypes

  • 据我所知,IE似乎从来没有一开始就支持它。

    09-25 17:29
    查看更多