我有一个窗体配置为更改网络设备上的IP地址,它都运行良好。我遇到的问题是如何处理客户端上的IP地址更改。我掀起了一些jQuery的本质,等待看看新的IP是否可达,如果它是浏览器转发,如果它不是浏览器显示新的IP信息,并让用户知道他们可能需要改变他们网络配置重新回到框中。jQuery AJAX脚本来更改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(" \
\
\
Unable to contact new IP address. \
\
You may need to change settings on your local machine in order to communicate with this address. \
\
IP Address - " + newIp + "\
Subnet Mask - " + newSnm + "\
Gateway - " + newGw + "\
\
");
},
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(); }
});
});