[关闭]
@wangyupu 2020-09-01T13:18:00.000000Z 字数 1045 阅读 32

ajax

javaweb


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("要返回的参数");
  1. url:要请求的路径(servlet路径)
  2. data:参数,以json形式传递
  3. dataType:返回数据格式,jsontexthtm格式
  4. typegetpost请求
  5. async:默认为true异步, false同步,true异步
  6. success:请求成功后回调函数
  7. result:服务器返回的参数
  8. error: 调用失败后回调的函数

引入jquery.

  1. click事件 $("#sub").click(function(){})
  2. 获取文本框内容 $("#...").val()
  3. ajax
  4. $.ajax({
  5. url:"",//路径
  6. data:{xxx:xxx,sss:sss},//参数
  7. dataType:"text" , //返回值类型
  8. type:"", //get请求或post请求
  9. success:function(result){//回调函数
  10. alert(result);
  11. }
  12. });
  13. 修改loginServlet
  14. 注释转发重定向代码
  15. 创建out对象
  16. PrintWriter out = response.getWriter();
  17. 返回参数 out.print("参数");
  18. ```
  19. #get请求
  20. ```
  21. $.get(URL,data,function(data,status,xhr),dataType);
  22. $.getJSON(url,data,success(data,status,xhr));
  23. $.get(URL请求路径,参数,function(result){
  24. },请求方式);
  25. $.getJSON(URL请求路径,参数,function(result){
  26. });
  27. <div class="md-section-divider"></div>

post请求

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