后台怎么接收ajax传过来的数据
前端有个href点击时触发Ajax将前台的一个值(比如说姓名)传给后台这个Ajax怎么写?后端又怎么接收这个值初学者求大神指教...
前端有个href 点击时触发Ajax将前台的一个值(比如说姓名)传给后台 这个Ajax怎么写?后端又怎么接收这个值 初学者求大神指教
展开
1个回答
展开全部
接收ajax传过来的数据
客户端代码:
<script type="text/javascript">
function checkUser(ouser){
var uname=ouser.value;
if(!uname){
alert("用户名不能为空");
ouser.focus;
}
//发送请求到服务器,判断用户名是否存在
//Ajax代码实现
// 发送请求到服务器,判断用户名是否存在
// 请求字符串
/ar url = "servlet/doReg?uname="+uname; //GET 方式
var url = "servlet/doReg"; //POST 方式
var userinfo="uname="+uname;
// 1. 创建XMLHttpRequest组件
xmlHttpRequest = createXmlHttpRequest();
// 2. 设置回调函数
xmlHttpRequest.onreadystatechange = haoLeJiaoWo;
// 3. 初始化XMLHttpRequest组件
//xmlHttpRequest.open("GET",url,true); //GET方式
xmlHttpRequest.open("POST",url,true);//POST方式
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//POST方式需要设置
// 4. 发送请求
xmlHttpRequest.send(userinfo);
}
function haoLeJiaoWo(){
if( xmlHttpRequest.readyState == 4
&& xmlHttpRequest.status == 200){
var b = xmlHttpRequest.responseText;
b=b.replace(/(^\s*)|(\s*$)/g,"");
if(b=="true"){
alert("用户名已经存在");
}else{
alert("用户名可以使用");
}
}
}
function createXmlHttpRequest(){
if(window.XMLHttpRequest){//新版本IE浏览器(IE7及以上版本)或其他浏览器
return new XMLHttpRequest();
}else { //老版本IE浏览器(包括IE5和IE6)
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
</script>
服务器端代码:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("textml;charset=utf-8");
PrintWriter out = response.getWriter();
String uname=request.getParameter("uname");
System.out.println(uname);
boolean uExists=false;
if(uname.equals("hdjr"))
{ uExists=true;
out.print(uExists);
}else{
out.print(uExists);
}
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
客户端代码:
<script type="text/javascript">
function checkUser(ouser){
var uname=ouser.value;
if(!uname){
alert("用户名不能为空");
ouser.focus;
}
//发送请求到服务器,判断用户名是否存在
//Ajax代码实现
// 发送请求到服务器,判断用户名是否存在
// 请求字符串
/ar url = "servlet/doReg?uname="+uname; //GET 方式
var url = "servlet/doReg"; //POST 方式
var userinfo="uname="+uname;
// 1. 创建XMLHttpRequest组件
xmlHttpRequest = createXmlHttpRequest();
// 2. 设置回调函数
xmlHttpRequest.onreadystatechange = haoLeJiaoWo;
// 3. 初始化XMLHttpRequest组件
//xmlHttpRequest.open("GET",url,true); //GET方式
xmlHttpRequest.open("POST",url,true);//POST方式
xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//POST方式需要设置
// 4. 发送请求
xmlHttpRequest.send(userinfo);
}
function haoLeJiaoWo(){
if( xmlHttpRequest.readyState == 4
&& xmlHttpRequest.status == 200){
var b = xmlHttpRequest.responseText;
b=b.replace(/(^\s*)|(\s*$)/g,"");
if(b=="true"){
alert("用户名已经存在");
}else{
alert("用户名可以使用");
}
}
}
function createXmlHttpRequest(){
if(window.XMLHttpRequest){//新版本IE浏览器(IE7及以上版本)或其他浏览器
return new XMLHttpRequest();
}else { //老版本IE浏览器(包括IE5和IE6)
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
</script>
服务器端代码:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("textml;charset=utf-8");
PrintWriter out = response.getWriter();
String uname=request.getParameter("uname");
System.out.println(uname);
boolean uExists=false;
if(uname.equals("hdjr"))
{ uExists=true;
out.print(uExists);
}else{
out.print(uExists);
}
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |