js 检测链接是否有效(包含跨域)
const checkUrl = function (url) { const promise = new Promise(function (resolve, reject) { if (!url) reject('无效路径'); $.ajax({ url: url, type: 'GET', dataType: "jsonp", //跨域采用jsonp方式 complete: (response)=> { if(response.status==200) resolve(true) else resolve(false) } }); }); return promise; }
checkUrl(url).then(res=>{
if(res){
alert('有效')
}else {
alert('无效')
}
});