接收ajax传过来的数据
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();
xmlHttpRequest.onreadystatechange = haoLeJiaoWo;
//xmlHttpRequest.open("GET",url,true); //GET方式
xmlHttpRequest.open("POST",url,true);//POST方式
xmlHttpRequest.send(userinfo);
function haoLeJiaoWo(){
var b = xmlHttpRequest.responseText;
if(b=="true"){
alert("用户名已经存在");
}else{
alert("用户名可以使用");
function createXmlHttpRequest(){
return new XMLHttpRequest();
return new ActiveXObject("Microsoft.XMLHTTP");
服务器端代码:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
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);
out.flush();
out.close();
/**
* The doPost method of the servlet.
*
* 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)
doGet(request,response);
前后端 前端:用户可以看见的界面,我们一般浏览网页时看到的界面,展现网页数据给用户看的地方. 后端:要将前端的数据存储到数据库中,并且把数据库的数据传递给前端. 数据库:存储数据的地方. 前后端交互:前端将数据传递给服务端,并且能够从服务端接收到返回的数据的过程.
回调函数设置一个data参数,用来接收返回值
$.ajax({
cache: false, //是否缓存
url: "?"+stype, //路径
success:function(data){ //成功后返回的回调方法
var json// data就是后台返回的数据
})
以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!