ajax的两种提交方式(get/post)和两种版本
程序员文章站
2022-06-04 23:13:02
最近比较闲,就把以前用过的技术串一下做个手札,方便以后自己偷懒,小鸟你们幸福了。 首先主要是将javascript版本ajax做下注释:ajax异步刷新主要是将所需条件拼成...
最近比较闲,就把以前用过的技术串一下做个手札,方便以后自己偷懒,小鸟你们幸福了。
首先主要是将javascript版本ajax做下注释:ajax异步刷新主要是将所需条件拼成字符串传入后台,处理之后,直接调用回调函数将所得数据返还给页面,并加以显示,因为还在本页面,所以不用刷新页面,懂了了吧,本篇也用encodeuri对字符串做了加密,并在类里做了解码,其中需要一些注意的地方在源码里做了注释。get/post两种提交方式,但get提交容易乱码,一定多加注意
jsp页面:
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>">
<script type="text/javascript" >
var xmlhttp;
function createxmlhttprequest(){
if(window.xmlhttprequest){
xmlhttp= new xmlhttprequest();//ie7+,firefox,opera,safari,chrome
}else{
xmlhttp=new activexobject("microsoft.xmlhttp");
}
}
function test(){//get
//获取参数
//var unames=encodeuri(document.getelementbyid("username").value);//一次编码java用new string(name.getbytes("iso8859-1"), "utf-8")解码
var unames=encodeuri(encodeuri(document.getelementbyid("username").value));//两次编码才能用java.net.urldecoder.decode(name,"utf-8");解码
var pws=encodeuri(document.getelementbyid("password").value);
createxmlhttprequest();
xmlhttp.onreadystatechange=readystate;
//function(){
//alert(xmlhttp.readystate+"=="+xmlhttp.status);//判断请求状态
//}
xmlhttp.open("get","ajaxservlet1?msg=gets&name="+unames+"&pwd="+pws+"×tamp="+new date().gettime(),true); //get 方式提交中文会出现乱码,encodeuri()/encodeuricomponent()将中文转成16进制编码,把字符串作为uri进行编码
xmlhttp.send(null);
}
function testp(){//post
//获取参数
var unames=document.getelementbyid("username").value;
var pws=document.getelementbyid("password").value;
createxmlhttprequest();
xmlhttp.onreadystatechange=readystate;
xmlhttp.open("post","ajaxservlet1",true);
xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded");
var str="msg=posts&name="+unames+"&pwd="+pws+"×tamp="+new date().gettime();
xmlhttp.send(str);//send 可用于传参
}
function readystate(){
if(xmlhttp.readystate==4){
if(xmlhttp.status==200){
var msg= xmlhttp.responsetext;
//alert(msg);
document.getelementbyid("result").innerhtml=msg;
}
}
}
</script>
<title>js异步刷新</title>
</head>
<body>
<center>
<div id="response">
</div>
用户:<input type="text" name="uname" id="username"><br>
密码:<input type="text" name="pw" id="password"><br>
<input type="button" name="button" value="get确定" onclick="test();"/>
<input type="button" name="button" value="post确定" onclick="testp();">
<div id="result">
</div>
</center>
</body>
</html>
这里是servlet/action java代码:
package com.cstp.javascript;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
@suppresswarnings("serial")
public class ajaxservlet1 extends httpservlet {
public void doget(httpservletrequest request, httpservletresponse response)throws servletexception, ioexception {
this.dopost(request, response);
}
public void dopost(httpservletrequest request, httpservletresponse response)throws servletexception, ioexception {
//设置编码,防止乱码
response.setcharacterencoding("utf-8");
request.setcharacterencoding("utf-8");
//接收参数
string msg=request.getparameter("msg");
if(msg.equals("gets")){
// string name=new string(request.getparameter("name").getbytes("iso8859-1"), "utf-8"); //一次编码,java里进行解码操作
string name=java.net.urldecoder.decode(request.getparameter("name"),"utf-8"); //decode解码页面必须两次编码,java里进行解码操作
string pwd=request.getparameter("pwd");
system.out.println(name+","+pwd);
printwriter out = response.getwriter();
out.println("ajax响应get,结果返回"+name+","+pwd);
}else if(msg.equals("posts")){
string name=new string(request.getparameter("name").getbytes("utf-8"), "utf-8"); //一次编码,java里进行解码操作
string pwd=request.getparameter("pwd");
system.out.println(name+","+pwd);
printwriter out = response.getwriter();
out.println("ajax响应post,结果返回"+name+","+pwd);
}
}
}
上面是javascript版ajax,下面将喜欢jquery版的也分享给jq友们:
页面上:
<script type="text/javascript">
//方式①
function circum(lon,lat){
$.ajax({
url: "jqajaxservlet?method=jsons",
contenttype: "application/x-www-form-urlencoded; charset=utf-8",
type : 'post',
datatype:"json",
async: false,
data : { //传参给后台
'lon' : lon,
'lat' : lat
},
success: function (data) { // 接后台返回result
在这里data为后台返回数据,你可以尽情处理了
}
});
}
</script>
后台:servlet/action里
类里对数据处理的方法同上,就不再累赘了
首先主要是将javascript版本ajax做下注释:ajax异步刷新主要是将所需条件拼成字符串传入后台,处理之后,直接调用回调函数将所得数据返还给页面,并加以显示,因为还在本页面,所以不用刷新页面,懂了了吧,本篇也用encodeuri对字符串做了加密,并在类里做了解码,其中需要一些注意的地方在源码里做了注释。get/post两种提交方式,但get提交容易乱码,一定多加注意
jsp页面:
复制代码 代码如下:
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>">
<script type="text/javascript" >
var xmlhttp;
function createxmlhttprequest(){
if(window.xmlhttprequest){
xmlhttp= new xmlhttprequest();//ie7+,firefox,opera,safari,chrome
}else{
xmlhttp=new activexobject("microsoft.xmlhttp");
}
}
function test(){//get
//获取参数
//var unames=encodeuri(document.getelementbyid("username").value);//一次编码java用new string(name.getbytes("iso8859-1"), "utf-8")解码
var unames=encodeuri(encodeuri(document.getelementbyid("username").value));//两次编码才能用java.net.urldecoder.decode(name,"utf-8");解码
var pws=encodeuri(document.getelementbyid("password").value);
createxmlhttprequest();
xmlhttp.onreadystatechange=readystate;
//function(){
//alert(xmlhttp.readystate+"=="+xmlhttp.status);//判断请求状态
//}
xmlhttp.open("get","ajaxservlet1?msg=gets&name="+unames+"&pwd="+pws+"×tamp="+new date().gettime(),true); //get 方式提交中文会出现乱码,encodeuri()/encodeuricomponent()将中文转成16进制编码,把字符串作为uri进行编码
xmlhttp.send(null);
}
function testp(){//post
//获取参数
var unames=document.getelementbyid("username").value;
var pws=document.getelementbyid("password").value;
createxmlhttprequest();
xmlhttp.onreadystatechange=readystate;
xmlhttp.open("post","ajaxservlet1",true);
xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded");
var str="msg=posts&name="+unames+"&pwd="+pws+"×tamp="+new date().gettime();
xmlhttp.send(str);//send 可用于传参
}
function readystate(){
if(xmlhttp.readystate==4){
if(xmlhttp.status==200){
var msg= xmlhttp.responsetext;
//alert(msg);
document.getelementbyid("result").innerhtml=msg;
}
}
}
</script>
<title>js异步刷新</title>
</head>
<body>
<center>
<div id="response">
</div>
用户:<input type="text" name="uname" id="username"><br>
密码:<input type="text" name="pw" id="password"><br>
<input type="button" name="button" value="get确定" onclick="test();"/>
<input type="button" name="button" value="post确定" onclick="testp();">
<div id="result">
</div>
</center>
</body>
</html>
这里是servlet/action java代码:
复制代码 代码如下:
package com.cstp.javascript;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
@suppresswarnings("serial")
public class ajaxservlet1 extends httpservlet {
public void doget(httpservletrequest request, httpservletresponse response)throws servletexception, ioexception {
this.dopost(request, response);
}
public void dopost(httpservletrequest request, httpservletresponse response)throws servletexception, ioexception {
//设置编码,防止乱码
response.setcharacterencoding("utf-8");
request.setcharacterencoding("utf-8");
//接收参数
string msg=request.getparameter("msg");
if(msg.equals("gets")){
// string name=new string(request.getparameter("name").getbytes("iso8859-1"), "utf-8"); //一次编码,java里进行解码操作
string name=java.net.urldecoder.decode(request.getparameter("name"),"utf-8"); //decode解码页面必须两次编码,java里进行解码操作
string pwd=request.getparameter("pwd");
system.out.println(name+","+pwd);
printwriter out = response.getwriter();
out.println("ajax响应get,结果返回"+name+","+pwd);
}else if(msg.equals("posts")){
string name=new string(request.getparameter("name").getbytes("utf-8"), "utf-8"); //一次编码,java里进行解码操作
string pwd=request.getparameter("pwd");
system.out.println(name+","+pwd);
printwriter out = response.getwriter();
out.println("ajax响应post,结果返回"+name+","+pwd);
}
}
}
上面是javascript版ajax,下面将喜欢jquery版的也分享给jq友们:
页面上:
复制代码 代码如下:
<script type="text/javascript">
//方式①
function circum(lon,lat){
$.ajax({
url: "jqajaxservlet?method=jsons",
contenttype: "application/x-www-form-urlencoded; charset=utf-8",
type : 'post',
datatype:"json",
async: false,
data : { //传参给后台
'lon' : lon,
'lat' : lat
},
success: function (data) { // 接后台返回result
在这里data为后台返回数据,你可以尽情处理了
}
});
}
</script>
后台:servlet/action里
类里对数据处理的方法同上,就不再累赘了