知道了!我已经解决了JSONP和jQuery AJAX调用的问题。我发现了
timeoutjQuery AJAX
的属性,并且当端口被阻塞或打开时,我的代码可以流畅地执行。这是未来访客的解决方案。感谢所有答复者的贡献。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript" src="jquery-1.7.2-min.js"></script> </head> <body> <script type"text/javascript"> var isAccessible = null; function checkConnection() { var url = "http://101.212.33.60:1935/test/hello.html" ; $.ajax({ url: url, type: "get", cache: false, dataType: 'jsonp', // it is for supporting crossdomain crossDomain : true, asynchronous : false, jsonpCallback: 'deadCode', timeout : 1500, // set a timeout in milliseconds complete : function(xhr, responseText, thrownError) { if(xhr.status == "200") { isAccessible = true; success(); // yes response came, esecute success() } else { isAccessible = false; failure(); // this will be executed after the request gets timed out due to blockage of ports/connections/IPs } } }); } $(document).ready( function() { checkConnection(); // here I invoke the checking function }); </script> </body></html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)