代码之家  ›  专栏  ›  技术社区  ›  HurnsMobile

jquery-ajax脚本更改IP地址-导致多个浏览器崩溃

  •  0
  • HurnsMobile  · 技术社区  · 15 年前

    我配置了一个表单来更改网络设备上的IP地址,它运行得很好。我面临的问题是如何处理客户端的IP地址更改之类的问题。我快速启动了一些jquery到Essential Sit,然后等待查看新的IP是否可以访问,是否是浏览器转发的,如果不是,浏览器将显示新的IP信息,并让用户知道他们可能需要更改网络配置才能返回框中。

    问题在于,有时此脚本会碰到 .ajax 意外调用,然后浏览器无法再加载该框上的任何页面。如果您使用任何其他浏览器,Web服务器仍然可以访问,因此它与框本身没有问题。如果关闭浏览器并重新加载页面,它就会工作。这使我相信在某个地方有一个循环,但我似乎找不到它。下面是我在客户端使用的内容。

    $(function(){
        var newIp;
        var newSnm;
        var newGw; 
        var ns1;
        var ns2; 
    
        $("#updateip").click(function(){
            newIp = $("#ipaddress").val();
            newSnm = $("#subnetmask").val();
            newGw  = $("#gateway").val();
            ns1 = $("#ns1").val();
            ns2 = $("#ns2").val();
    
            $status.dialog('open');
            $.ajax({
                type: "POST",
                url: "setuphandler.php",
                data: { updateip : "false", ipaddress : newIp , subnetmask : newSnm, gateway : newGw, ns1 : ns1, ns2 : ns2},
                timeout: 10000, //If the post doesnt finish in under 10 seconds this will force an error event
                success: //This will fire if the IP was not changed by the post
                    function(xhr, data){
                        $status.dialog('close');
                    },
                error: //This will fire if the IP address has been changed by the post
                    function(){
                        checkNewURL();
                    }
                });
        });
    
        //Function to forward the browser if the new IP is reachable, if not alert the user and display the new settings to them 
        function checkNewURL(){
            $.ajax({
                type: "GET",
                timeout: 700,
                cache: false,
                url: 'http://' + newIp,
                error: //if the new ip is not reachable, display new connection info to the user 
                    function(){
                        $status.dialog( "option", "title", "Error" );
                        $status.dialog( "option", "height", 300 );
                        $status.html(" \
                                    <small> \
                                        <br />\
                                        <b>Unable to contact new IP address.</b> \
                                        <br />\
                                        You may need to change settings on your local machine in order to communicate with this address. \
                                        <br />\
                                        IP Address - " + newIp + "\
                                        Subnet Mask - " + newSnm + "\
                                        Gateway - " + newGw + "\
                                    </small> \
                                    ");
                    },
                success: //If the new ip is reachable, auto-forward the user
                    function(){
                        $status.html("Redirecting...");
                        redirectUrl = 'http://' + newIp + '/setup.php'
                        setTimeout("window.location=redirectUrl", 2000);
                    }
            });
        }
    
        $status.dialog({
            modal: true,
            height: 225,
            width: 350,
            title: 'Applying Changes',
            autoOpen: false,
            draggable: false,
            resizable: false,
            closeOnEscape: false,
            open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
        });
    
    });
    
    2 回复  |  直到 15 年前
        1
  •  4
  •   meder omuraliev    15 年前

    这不是答案,但你应该改变

                    setTimeout("window.location=redirectUrl", 2000);
    

                    setTimeout(function() { window.location=redirectUrl}, 2000);
    

    向该函数发送文本字符串是非常糟糕的做法,因为它速度较慢,而且不如函数文本好。

        2
  •  0
  •   HurnsMobile    15 年前

    这是一个非常不相关的问题/答案。在测试中,我输入了名称服务器的随机IP。这不会是一个问题,除非框上有一个初始化脚本,该脚本将主机名更改为但不将其添加到 /etc/hosts 因此,机器无法自行进行正确的查找。 localhost 地址。结果,这打破了sudo!不管怎样,谢谢大家的关注!