Ajax简单的异步请求以及返回的值获取
1个回答
2017-06-25 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
JS里面写ajax发送请求到页面,页面返回数据提示结果:
--------------------------------------------------JS代码----------------------------------------------------
function trimRepID(s){return s.replace(/^\s+|\s+$/g,"");} //去点字符串的空格的函数
---encodeURI();//UTF-8编码的转换
//先声明一个异步请求对象
var xmlHttpReg = null;
if (window.ActiveXObject) {
//如果是IE
xmlHttpReg = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttpReg = new XMLHttpRequest();
//实例化一个xmlHttpReg
}
//发送请求 方式、路径、权限
xmlHttpReg.open("POST","http://******.jsp?DDD="+repId, true);
//设置头部信息,让它用utf-8发送,不然中文可能会乱码
xmlHttpReg.setrequestheader("content-type","application/x-www-form-urlencoded");
xmlHttpReg.onreadystatechange = function ()
{
if (xmlHttpReg.readyState == 4 && xmlHttpReg.status == 200)
{ }
else{}
}
xmlHttpReg.send(null);
alert("JSOn");//
var flags=xmlHttpReg.responseText;
alert(flags);//返回结果的responseText
var flagf=JSON.stringify(flags);
alert(flagf);//OBJECT转化成STRING
if(flagf.indexOf("tu")>0){alert(" SUCCESSFULLY");} //对结果进行搜索结果,匹配返回值的位置,有即表示成功吗,无就是失败
else{alert("FAILED TO SAVE");}
}
---------------jsp操作数据代码------------------------------------
<script>
window.onload=
function () {
alert("IDBC BE");
<%
System.out.println("Connections");
//获取AJAX传递过来的参数
String operID= request.getParameter("operID");
System.out.println(operID);
response.setContentType("text/html");
PrintWriter outa = response.getWriter();
if(operID!=null){
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@***:**:**";
//orcl为你的数据库的SID
String user = "***";
String password = "***";
//用户名和密码
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//建立链接
String sql = "";
//插入数据库的语句
ResultSet rs = stmt.executeQuery(sql);
String result="tu";
rs.close();
stmt.close();
conn.close();
System.out.println(result);
outa.print(result);
} catch (Exception e) {
//返回异常信息
e.printStackTrace();
String result="fa";
outa.print(result);
}}
%>
}
</script>
--------------------------jsp代码写在了js中-----------
返回成功----tu 失败---fa
--------------------------------------------------JS代码----------------------------------------------------
function trimRepID(s){return s.replace(/^\s+|\s+$/g,"");} //去点字符串的空格的函数
---encodeURI();//UTF-8编码的转换
//先声明一个异步请求对象
var xmlHttpReg = null;
if (window.ActiveXObject) {
//如果是IE
xmlHttpReg = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
xmlHttpReg = new XMLHttpRequest();
//实例化一个xmlHttpReg
}
//发送请求 方式、路径、权限
xmlHttpReg.open("POST","http://******.jsp?DDD="+repId, true);
//设置头部信息,让它用utf-8发送,不然中文可能会乱码
xmlHttpReg.setrequestheader("content-type","application/x-www-form-urlencoded");
xmlHttpReg.onreadystatechange = function ()
{
if (xmlHttpReg.readyState == 4 && xmlHttpReg.status == 200)
{ }
else{}
}
xmlHttpReg.send(null);
alert("JSOn");//
var flags=xmlHttpReg.responseText;
alert(flags);//返回结果的responseText
var flagf=JSON.stringify(flags);
alert(flagf);//OBJECT转化成STRING
if(flagf.indexOf("tu")>0){alert(" SUCCESSFULLY");} //对结果进行搜索结果,匹配返回值的位置,有即表示成功吗,无就是失败
else{alert("FAILED TO SAVE");}
}
---------------jsp操作数据代码------------------------------------
<script>
window.onload=
function () {
alert("IDBC BE");
<%
System.out.println("Connections");
//获取AJAX传递过来的参数
String operID= request.getParameter("operID");
System.out.println(operID);
response.setContentType("text/html");
PrintWriter outa = response.getWriter();
if(operID!=null){
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url = "jdbc:oracle:thin:@***:**:**";
//orcl为你的数据库的SID
String user = "***";
String password = "***";
//用户名和密码
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement
(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//建立链接
String sql = "";
//插入数据库的语句
ResultSet rs = stmt.executeQuery(sql);
String result="tu";
rs.close();
stmt.close();
conn.close();
System.out.println(result);
outa.print(result);
} catch (Exception e) {
//返回异常信息
e.printStackTrace();
String result="fa";
outa.print(result);
}}
%>
}
</script>
--------------------------jsp代码写在了js中-----------
返回成功----tu 失败---fa
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询