很简单,只需使用HEAD方法,而不是GET或POST:
function UrlExists(url, callback){ var http = new XMLHttpRequest(); http.open('HEAD', url); http.onreadystatechange = function() { if (this.readyState == this.DONE) { callback(this.status != 404); } }; http.send();}
这只是一个简短的示例,展示了如何使用HEAD方法。生产代码可能需要针对不同的结果状态(成功,失败,超时)更细粒度的回调,并可能使用不同的事件处理程序(
onload,
onerror而
ontimeout不是
onreadystatechange)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)