跨域监测远程链接URL是否可访问

使用jquery的$.ajax来实现远程验证链接是否可用,验证网站服务器是否可用,其中dataType: "jsonp"必须的,类型必须是jsonp才能实现跨域。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
       $.ajax({
                        type: 'get',
                        url: 'http://www.mgstage.com/img/pc/logo.jpg',
                        cache: false,
                        dataType: "jsonp", //跨域采用jsonp方式
                        processData: false,
                        timeout:10000, //超时时间,毫秒
                        complete: function (data) {
                            if (data.status==200) {
                                alert("成功")
                            } else {
                                alert("失败")
                            }
                        }
                    });

jsonp

 

完整版:用户输入域名开始验证,可实现返回时间等信息。

检验的url地址:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<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>检验的url地址:</div>
  <input type="text" style="width:600px;height:30px;font-size:14px;" id="urlText" value="https://www.baidu.com/" />
    <input type="button" value="判断是否可访问" 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
      var dateTime = disptime();
      var time2 = dateTime.DateTime; 
      $("#msg1").html("发送时间:" + time2); 
      $.ajax({
        type: 'get',
        url: url,
        cache: false,
        dataType: "jsonp", //跨域采用jsonp方式 
        processData: false,
        timeout:10000, //超时时间,毫秒
        complete: function (data) {
          var dateTime2 = disptime();
          var time22 = dateTime2.DateTime;
          var htmlTxt =[];
          if (data.status==200) {
            htmlTxt.push("成功<br/>");
          } else {
            htmlTxt.push("失败<br/>");
          }        
          htmlTxt.push("readyState=" + data.readyState + "<br/>status=" + data.status + "<br/>statusText=" + data.statusText + "<br/>响应时间:" + time22);
          var htmlString = htmlTxt.join('');
          $("#msg").html(htmlString);
        }       
      });
    }
    function disptime() {
      var date = new Date();
      var yyyy = date.getFullYear();//四位年份
      var month = date.getMonth() + 1;//月份 0-11
      var day = date.getDate();//日
      var HH = date.getHours();//时
      var minute = date.getMinutes();//分钟
      var second = date.getSeconds();//秒
      var milliseconds=date.getMilliseconds();//毫秒
      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>

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: