如何检查服务器上的文件是否存在jQuery或纯JavaScript中?
With jQuery:(使用jQuery:)
$.ajax({ url:'http://www.example.com/somefile.ext', type:'HEAD', error: function() { //file not exists }, success: function() { //file exists } });
function UrlExists(url) { var http = new XMLHttpRequest(); http.open('HEAD', url, false); http.send(); return http.status!=404; }
1.4m articles
1.4m replys
5 comments
57.0k users