@wangzhuanyun
2020-07-14T03:25:31.000000Z
字数 996
阅读 1922
web
AJAX基本使用
//jsp或html页面
$.ajax({
url:"servlet/IndexServlet",
data:{name:"张三"},
dataType:"text",
type:"post",
async:false,
success:function (result) {
alert(result);
},
error:function(){
}
});
//java中servlet
PrintWriter out = response.getWriter();
out.print("要返回的参数");
url:要请求的路径(servlet路径)
data:参数,以json形式传递
dataType:返回数据格式,json,text,htm格式
type:get或post请求
async:默认为true异步, false同步,true异步
success:请求成功后回调函数
result:服务器返回的参数
error: 调用失败后回调的函数
引入jquery
写click事件 $("#sub").click(function(){})
获取文本框内容 $("#...").val()
写ajax
$.ajax({
url:"",//路径
data:{xxx:xxx,sss:sss},//参数
dataType:"text" , //返回值类型
type:"", //get请求或post请求
success:function(result){//回调函数
alert(result);
}
});
修改loginServlet
注释转发重定向代码
创建out对象
PrintWriter out = response.getWriter();
返回参数 out.print("参数");
get请求
$.get(URL,data,function(data,status,xhr),dataType);
$.getJSON(url,data,success(data,status,xhr));
$.get(URL请求路径,参数,function(result){
},请求方式);
$.getJSON(URL请求路径,参数,function(result){
});
post请求
$.post(URL,data,function(data,status,xhr),dataType);
$.post(URL请求路径,参数,function(result){
},请求方式);