gpt4 book ai didi

javascript - ajax调用后刷新整个页面

转载 作者:行者123 更新时间:2023-11-28 19:29:58 24 4
gpt4 key购买 nike

我想在不使用 jquery 的情况下执行 ajax 调用后重新加载同一页面。这可能吗?在我的代码中,我要删除表中的数据库名称。删除数据库名称后,页面不显示数据库名称。这可能吗?我用 window.location="Databases.jsp"尝试过,但没有工作。我可以做什么来执行此任务?而且我正在显示包含框架集内的数据库名称的表。因此,当我更改/删除数据库名称时,我也想刷新左侧框架集。(也就是说,我需要刷新整个页面)我的代码:

数据库.jsp

    <%-- 
Document : Databases
Created on : Nov 10, 2014, 3:08:01 PM
Author : user
--%>

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DatabaseMetaData"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="querywork.DBConnection"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
var request;

function drop()
{

var cboxes = document.getElementsByName('ch');
var len = cboxes.length;
var dbnam = document.getElementById("hid") .value
var dbs="";
var dbname=dbnam.split(",")
for (var i=0; i<len; i++) {
if(cboxes[i].checked)
{
dbs= dbname[i]

var url="Delete.jsp?val="+dbs;

if(window.XMLHttpRequest){
request=new XMLHttpRequest();
}
else if(window.ActiveXObject){
request=new ActiveXObject("Microsoft.XMLHTTP");
}

try
{
request.onreadystatechange=getInfo;
request.open("GET",url,true);
request.send();
}
catch(e)
{
alert("Unable to connect to server");
}
}
}
}

function getInfo(){
if(request.readyState==4){
var val=request.responseText;
document.getElementById('amit').innerHTML=val;

}
}

</script>
</head>


<body>
<form id="form1" name="form1" method="post" action="">

<h2>Databases</h2>
<label>
<input type="text" name="textfield" value="Database Name" id="dbname" />
</label>
<label>
<input type="submit" name="Submit" value="CREATE" onClick="create(textfield.value)"/>

</label>
<p>&nbsp;</p>



<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");

DatabaseMetaData dbmd = conn.getMetaData();

ResultSet ctlgs = dbmd.getCatalogs();

%>


<table border="1" cellpadding ="2"cellspacing="3">
<tr>
<td>

</td>
<td>
Database Name
</td>

</tr>
<%
String dbnme = "";
int i = 0;

while (ctlgs.next()) {
dbnme = dbnme + ctlgs.getString(1) + ",";
String name = "ch" + i;
%>

<tr>
<td>
<input name="ch" type="checkbox" value="" id="<%=name%>"/>
</td>
<td>
<%=ctlgs.getString(1)%>
</td>
</tr>
<%
i++;
}%>
</table>
<input type="hidden" name="hid" id="hid1" value="<%=i%>"/>
<input type="hidden" name="hid" id="hid" value="<%=dbnme%>"/>
<%
} catch (Exception e) {
out.println(e);
}%>
<font color="blue"><u><label id="lid" onclick="drop()"> Drop</label></u></font>
<br></br>
<div id="amit"></div>
</form>


</body>

删除.jsp

<%-- 
Document : Delete
Created on : Nov 11, 2014, 9:43:22 PM
Author : user
--%>

<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="querywork.DBConnection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
String responseText = "";
String message = "";
String dbname = request.getParameter("val");
boolean flg = false;
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");
PreparedStatement ps = conn.prepareStatement("DROP DATABASE " + dbname);
int k = ps.executeUpdate();
if (k > 0) {
flg = true;
} else {
flg = false;
}

if (flg) {
message = "success";
} else {
message = "failed";
}
// }
responseText = "Deleted";
out.println(responseText);
//response.sendRedirect("index.html");

%>

<input type="hidden" id="hid" value="<%=message%>"/>
<script>
var msg=document.getElementById("hid"). value
alert(msg)
window.location='Databases.jsp';

</script>

最佳答案

试试这个

location.reload();

window.location.reload()

如果还是不行,这里还有一个

window.location.href=window.location.href

关于javascript - ajax调用后刷新整个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27042450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com