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('無效') }});