Home > Web Front-end > JS Tutorial > js access public network server instance

js access public network server instance

PHP中文网
Release: 2017-06-19 17:39:03
Original
1017 people have browsed it

The source code used in the wifi authentication development process is as follows:

Note: Check whether the AC release is successful and whether the public network Alibaba Cloud server can be accessed

Function call :

checkNet().then(function(res) {
    if(res) {
        //连网成功        
    }
    } else {
        //连网失败                
    }
})
Copy after login

Function code:

(function() {
    //检测入口
    checkNet = function() {
        var def = $.Deferred();
        xiuNet(null).then(function(res) {
            netWork(null).then(function(res) {
                if(res) {
                    def.resolve(true);
                } else {
                    def.resolve(false);
                }
            })
        })
        return def.promise();
    }
    
    //嗅探网络,解决ios弹框接入网络问题
    var checkTime = 2; //嗅探次数
    xiuNet = function(def) {
        if(def == null) {def = $.Deferred();}
        var iframe = document.createElement("iframe");
        iframe.style.cssText = "display:none;width:0px;height:0px;";
        document.body.appendChild(iframe);
        var ua = navigator.userAgent;
        if(ua.indexOf("iPhone") != -1 || ua.indexOf("iPod") != -1 ||ua.indexOf("iPad") != -1) {
            iframe.src = "http://www.baidu.com" + "?timestap=" + new Date().getTime();
            console.info("嗅探网络----");
            if(checkTime > 1) {
                checkTime--;
                setTimeout(function() {
                    xiuNet(def)
                }, 1000);
            } else {
                def.resolve(true);
            }
        } else {
            def.resolve(false);
        }
        return def.promise();
    }

    //检测网络,是否可以连接公网
    var cnetTime = 3; //检测网络次数
    netWork = function(def) {
        if(def == null) {def = $.Deferred();}
        $.ajax({
            type: "get",
            timeout: 2000,
            dataType: "jsonp",
            jsonp: "callback",
            url: 'https://jktv.tv/shphone/ver.txt',
            success: function(data) {},
            error: function(xhr) {
                $("#ddd").html("status:"+xhr.status)
                if(xhr.readyState == 4 && xhr.status == 200) {
                    def.resolve(true);
                } else {
                    if(cnetTime > 1) {
                        cnetTime--;
                        setTimeout(function() {
                            netWork(def)
                        }, 200);
                    } else {
                        def.resolve(false);
                    }
                }
            }
        });
        return def.promise();
    }
})();
Copy after login

The above is the detailed content of js access public network server instance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template