@wangzhuanyun
2020-07-14T03:25:31.000000Z
字数 996
阅读 2028
web
AJAX基本使用
//jsp或html页面$.ajax({url:"servlet/IndexServlet",data:{name:"张三"},dataType:"text",type:"post",async:false,success:function (result) {alert(result);},error:function(){}});//java中servletPrintWriter 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){},请求方式);