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

JavaScript判断请求的URL是否可访问,支持跨域判断的实现方法

Come mostrato di seguito:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <meta name="keywords" content="Verifica URL con JavaScript" />
  <title>Verifica URL con JavaScript</title>
</head>
<body>
  <div>Indirizzo URL da verificare:</div>
  <input type="text" style="width:600px;height:30px;font-size:14px;" id="urlText" value="https://www.baidu.com/" />
  <input type="button" value="Verifica se Accessibile" onclick="getURL()" />
  <br />
  <div id="msg1"></div>
  <div id="msg"></div>
  <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
  <script type="text/javascript">
    function getURL() {
      $("#msg").html("");
      var url = $("#urlText").val(); // URL della richiesta
      var dateTime = disptime();
      var time2 = dateTime.DateTime; 
      $("#msg1").html("ora di invio: " + time2); 
      $.ajax({
        type: 'get',
        url: url,
        cache: false,
        dataType: "jsonp", // per superare i problemi di dominio, utilizzare jsonp 
        processData: false,
        timeout:10000, // tempo di scadenza, millisecondi
        complete: function (data) {
          var dateTime2 = disptime();
          var time22 = dateTime2.DateTime;
          var htmlTxt =[];
          if (data.status==200) {
            htmlTxt.push("riuscito<br/>");
          } else {
            htmlTxt.push("fallito<br/>");
          }        
          htmlTxt.push("readyState=" + data.readyState + "<br/>status=" + data.status + "<br/>statusText=" + data.statusText + "<br/>tempo di risposta: " + time22);
          var htmlString = htmlTxt.join('');
          $("#msg").html(htmlString);
        }       
      });
    }
    function disptime() {
      var date = new Date();
      var yyyy = date.getFullYear(); // quattro cifre di anno
      var month = date.getMonth() + 1; // Mese 0-11
      var day = date.getDate(); // Giorno
      var HH = date.getHours(); // Ore
      var minute = date.getMinutes(); // Minuti
      var second = date.getSeconds(); // Secondi
      var milliseconds=date.getMilliseconds(); // Millisecondi
      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> 

Il seguente articolo su come determinare se l'URL della richiesta JavaScript è accessibile, il metodo di implementazione che supporta la transizione a dominio è tutto il contenuto condiviso dall'autore. Spero che questo possa essere un riferimento per voi e spero che possiate sostenere Tutorial di url.