js判断请求的url是否可访问,支持跨域判断的实现方法
人气:0如下所示:
<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>
以上这篇js判断请求的url是否可访问,支持跨域判断的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
加载全部内容