English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Método de verificação de acessibilidade da URL solicitada, suportando verificação de cross-domain (implementação)

如下所示:

<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>Endereço da URL a ser testada:</>
  <input type="text" style="width:600px;height:30px;font-size:14px;" id="urlText" value="https://www.baidu.com/" />
    <input type="button" value="Verificar se é acessível" onclick="getURL()" />
  <br />
  <div id="msg1></>
  <div id="msg"></>
  <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("Tempo de envio:" + time2); 
      $.ajax({
        type: 'get',
        url: url,
        cache: false,
        dataType: "jsonp", //Para domínios cruzados usar jsonp 
        processData: false,
        timeout:10000, //Tempo de timeout, milissegundos
        complete: function (data) {
          var dateTime2 = disptime();
          var time22 = dateTime2.DateTime;
          var htmlTxt =[];
          if (data.status==200) {
            htmlTxt.push("sucesso<br/)");
          } else {
            htmlTxt.push("falha<br/)");
          }        
          htmlTxt.push("readyState=" + data.readyState + "<br/>status=" + data.status + "<br/>statusText=" + data.statusText + "<br/>Tempo de resposta:" + time22);
          var htmlString = htmlTxt.join('');
          $("#msg").html(htmlString);
        }       
      });
    }
    function disptime() {
      var date = new Date();
      var yyyy = date.getFullYear();//ano de quatro dígitos
      var month = date.getMonth() + 1;//mês 0-11
      var day = date.getDate();//dia
      var HH = date.getHours();//hora
      var minute = date.getMinutes();//minuto
      var second = date.getSeconds();//segundo
      var milliseconds=date.getMilliseconds();//milissegundos
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      if (HH < 10) {
        HH = "0" + HH;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      if (second < 10) {
        second = "0" + second;
      }
      var time = yyyy + "-" + month + "-" + day + " " + HH + : + minute + : + second + " " + milliseconds;
      var timeTxt = yyyy + month + day + HH + minute + second;
      var time = {
        DateTime: time,
        TimeTxt: timeTxt
      }
      return time;
    }
  </script>
</body>
</html> 

O seguinte artigo sobre como julgar se a URL de js é acessível, o método de implementação de suporte a cross-domain que o autor compartilha com vocês é tudo o que compartilho. Espero que isso possa oferecer uma referência para vocês, e espero que vocês apoiem mais o tutorial gritar.

Você também pode gostar