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(‘无效‘) } });
原文:https://www.cnblogs.com/-mrl/p/13630645.html