简体   繁体   English

XMLHttpRequest:确定链接是否不会返回4xx状态的最快方法?

[英]XMLHttpRequest: Fastest way to determine if a link won't return a 4xx status?

I'm currently sending a 'GET' request through XMLHttpRequest in Javascript, and looking at the returned status to determine if the links are valid and don't return a 4xx error. 我目前正在通过Javascript中的XMLHttpRequest发送“ GET”请求,并查看返回的状态以确定链接是否有效并且不返回4xx错误。 However, the links I'm looking at are 10sec-1min+ videos, and I'd rather not wait for the server response. 但是,我正在查看的链接是10秒至1分钟以上的视频,我宁愿不等待服务器响应。

I had a small bug in my project where all valid requests were returning 0, and invalid were returning 403. This was due to cross-origin header stuff that was caused from some invalid permissions in my manifest.json. 我的项目中有一个小错误,所有有效请求均返回0,无效请求返回403。这是由于跨源标头的原因,这是由于manifest.json中的某些无效权限引起的。 Anyway, this was actually semi-ideal, as I could just "assume" the requests returning 0 are valid and 403 are invalid. 无论如何,这实际上是半理想的,因为我可以“假设”返回0的请求有效,而403则无效。 There is further validation on my server that the links are sent to, so I wouldn't be entirely worried about that. 链接发送到我的服务器上还有进一步的验证,因此我不会完全为此担心。 This is just for some front end functionality. 这仅用于某些前端功能。 However, valid links weren't consistently returning 0 (sometimes 200), so I want to find an actual fix for this. 但是,有效链接并不是始终返回0(有时是200),因此我想为此找到一个实际的解决方法。 Is there any way around this? 有没有办法解决? Thanks! 谢谢!

EDIT: The links are directly to web hosted mp4 files 编辑:链接直接到网络托管的mp4文件

In almost cases, you just need to wait until the XMLHttpRequest.readyState becomes HEADERS_RECEIVED (2) instead of DONE (4). 在大多数情况下,您只需要等待XMLHttpRequest.readyState变为HEADERS_RECEIVED (2)而不是DONE (4)。 You can also use method HEAD instead of GET . 您也可以使用HEAD方法代替GET方法。

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

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