简体   繁体   中英

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. However, the links I'm looking at are 10sec-1min+ videos, and I'd rather not wait for the server response.

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. Anyway, this was actually semi-ideal, as I could just "assume" the requests returning 0 are valid and 403 are invalid. 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. Is there any way around this? Thanks!

EDIT: The links are directly to web hosted mp4 files

In almost cases, you just need to wait until the XMLHttpRequest.readyState becomes HEADERS_RECEIVED (2) instead of DONE (4). You can also use method HEAD instead of GET .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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