English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
如下所示:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="keywords" content="js判断URL是否可访问" /> <title>js判断URL是否可访问</title> </head> <body> <div>Dirigir URL de prueba:</div> <input type="text" style="width:600px;height:30px;font-size:14px;" id="urlText" value="https://www.baidu.com/" /> <input type="button" value="Comprobar si es accesible" onclick="getURL()" /> <br /> <div id="msg1></div> <div id="msg"></div> <script type="text/javascript" src="js/jquery-1.10.2script"></script> <script type="text/javascript"> function getURL() { $("#msg").html(""); var url = $("#urlText").val();//URL solicitada var dateTime = disptime(); var time2 = dateTime.DateTime; $("#msg1").html("Tiempo de envío: " + time2); $.ajax({ type: 'get', url: url, cache: false, dataType: "jsonp", //Usar jsonp para peticiones cruzadas processData: false, timeout:10000, //Tiempo de espera, en milisegundos complete: function (data) { var dateTime2 = disptime(); var time22 = dateTime2.DateTime; var htmlTxt =[]; if (data.status==200) { htmlTxt.push("éxito<br/)"); } else { htmlTxt.push("fallido<br/)"); } htmlTxt.push("readyState=" + data.readyState + "<br/>status=" + data.status + "<br/>statusText=" + data.statusText + "<br/>Tiempo de respuesta: " + time22); var htmlString = htmlTxt.join(''); $("#msg").html(htmlString); } }); } function disptime() { var date = new Date(); var yyyy = date.getFullYear();//año de cuatro dígitos var mes = date.getMonth() + 1;//mes 0-11 var día = date.getDate();//día var HH = date.getHours();//hora var minuto = date.getMinutes();//minuto var segundo = date.getSeconds();//segundo var milliseconds=date.getMilliseconds();//milisegundos if (mes < 10) { mes = "0" + mes; } if (día < 10) { día = "0" + día; } if (HH < 10) { HH = "0" + HH; } if (minuto < 10) { minuto = "0" + minuto; } if (segundo < 10) { segundo = "0" + segundo; } var time = yyyy + "-" + mes + "-" + día + " " + HH + : + minuto + : + segundo + " " + milisegundos; var timeTxt = yyyy + mes + día + HH + minuto + segundo; var time = { DateTime: time, TimeTxt: timeTxt } volver tiempo; } </script> </body> </html>
La siguiente entrada sobre cómo verificar si una URL de solicitud de JavaScript es accesible, con un método de implementación de apoyo para la verificación de cors, es todo lo que el autor ha compartido con ustedes. Espero que les sea útil como referencia y esperamos que apoyen a la serie de tutoriales Niyao.