[关闭]
@wangzhuanyun 2020-07-14T03:25:31.000000Z 字数 996 阅读 1922

AJAX

web


AJAX基本使用

  1. //jsp或html页面
  2. $.ajax({
  3. url:"servlet/IndexServlet",
  4. data:{name:"张三"},
  5. dataType:"text",
  6. type:"post",
  7. async:false,
  8. success:function (result) {
  9. alert(result);
  10. },
  11. error:function(){
  12. }
  13. });
  14. //java中servlet
  15. PrintWriter out = response.getWriter();
  16. out.print("要返回的参数");

url:要请求的路径(servlet路径)
data:参数,以json形式传递
dataType:返回数据格式,json,text,htm格式
type:get或post请求
async:默认为true异步, false同步,true异步
success:请求成功后回调函数
result:服务器返回的参数
error: 调用失败后回调的函数

  1. 引入jquery
  2. click事件 $("#sub").click(function(){})
  3. 获取文本框内容 $("#...").val()
  4. ajax
  5. $.ajax({
  6. url:"",//路径
  7. data:{xxx:xxx,sss:sss},//参数
  8. dataType:"text" , //返回值类型
  9. type:"", //get请求或post请求
  10. success:function(result){//回调函数
  11. alert(result);
  12. }
  13. });
  14. 修改loginServlet
  15. 注释转发重定向代码
  16. 创建out对象
  17. PrintWriter out = response.getWriter();
  18. 返回参数 out.print("参数");

get请求

$.get(URL,data,function(data,status,xhr),dataType);
$.getJSON(url,data,success(data,status,xhr));

  1. $.get(URL请求路径,参数,function(result){
  2. },请求方式);
  3. $.getJSON(URL请求路径,参数,function(result){
  4. });

post请求

$.post(URL,data,function(data,status,xhr),dataType);

  1. $.post(URL请求路径,参数,function(result){
  2. },请求方式);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注