我配置了一个表单来更改网络设备上的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(); }
});
});