您实际上并不需要ajax,只需创建一个video元素,然后查看它是否可以加载源
var video = document.createElement('video');video.onload = function() { alert('success, it exsist'); // show video element}video.onerror = function() { alert('error, couldn't load'); // don't show video element}video.src = 'http://www.example.com/video.mp4';不同的浏览器播放不同的格式,要检查文件是否可以在当前浏览器中播放,可以使用
canplaythrough事件
video.oncanplaythrough = function() { alert("This file can be played in the current browser");};如果文件在同一个域中,并且端口和协议匹配,则可以使用ajax进行HEAD请求,以查看资源是否存在,但是跨域不起作用
var http = new XMLHttpRequest();http.open('HEAD', '/folder/video.mp4');http.onreadystatechange = function() { if (this.readyState == this.DONE) { if (this.status != 404) { // resource exists } }};http.send();


