• 使用ajax()方法和后台交互


      ajax()方法是jQuery底层的ajax实现,通过HTTP请求加载远程数据。

      

     1 $.ajax({
     2         type: "GET",
     3         url: "handleAjaxRequest.action",
     4         data: {paramKey:paramValue},
     5         async: true,
     6         dataType:"json",
     7         success: function(returnedData) {
     8             alert(returnedData);
     9             //请求成功后的回调函数
    10             //returnedData--由服务器返回,并根据 dataType 参数进行处理后的数据;
    11             //根据返回的数据进行业务处理
    12         },
    13         error: function(e) {
    14             alert(e);
    15             //请求失败时调用此函数
    16         }
    17     });
    18 }

      参数说明:

      type:请求方式,“POST”或者“GET”,默认为“GET”。

      url:发送请求的地址。

      data:要向服务器传递的数据,已key:value的形式书写(id:1)。GET请求会附加到url后面。

      async:默认true,为异步请求,设置为false,则为同步请求。

      dataType:预期服务器返回的数据类型,可以不指定。有xml、html、text等。

      在开发中,使用以上参数已可以满足基本需求。

      如果需要向服务器传递中文参数,可将参数写在url后面,用encodeURI编码就可以了。

      

     1 var chinese = "中文";
     2 var urlTemp = "handleAjaxRequest.action?chinese="+chinese;
     3 var url = encodeURI(urlTemp);//进行编码
     4 
     5 $.ajax({
     6         type: "GET",
     7         url: url,//直接写编码后的url
     8         success: function(returnedData) {
     9             alert(returnedData);
    10             //请求成功后的回调函数
    11             //returnedData--由服务器返回,并根据 dataType 参数进行处理后的数据;
    12             //根据返回的数据进行业务处理
    13         },
    14         error: function(e) {
    15             alert(e);
    16             //请求失败时调用此函数
    17         }
    18     });
    19 }


      struts2的action对请求进行处理:

      

     1 public void handleAjaxRequest() {
     2         HttpServletRequest request = ServletActionContext.getRequest();
     3         HttpServletResponse response = ServletActionContext.getResponse();
     4         //设置返回数据为html文本格式
     5         response.setContentType("text/html;charset=utf-8");
     6         response.setHeader("pragma", "no-cache");
     7         response.setHeader("cache-control", "no-cache");
     8         PrintWriter out =null;
     9         try {
    10             String chinese = request.getParameter("chinese");
    11             //参数值是中文,需要进行转换
    12             chinese = new String(chinese.getBytes("ISO-8859-1"),"utf-8");
    13             System.out.println("chinese is : "+chinese);
    14             
    15             //业务处理
    16             
    17             String resultData = "hello world";
    18             out = response.getWriter();
    19             out.write(resultData);
    20             //如果返回json数据,response.setContentType("application/json;charset=utf-8");
    21             //Gson gson = new Gson();
    22             //String result = gson.toJson(resultData);//用Gson将数据转换为json格式
    23             //out.write(result);
    24        out.flush();
    25             
    26         }catch(Exception e) {
    27           e.printStackTrace();
    28         }finally {
    29             if(out != null) {
    30           out.close();
    31        }
    32      }
    33     }

      struts.xml配置文件:不需要写返回类型

      

    1 <action name="handleAjaxRequest" class="com.test.TestAction"
    2                 method="handleAjaxRequest">
    3  </action>

      

  • 相关阅读:
    python搭建开发环境
    django初探
    linux下安装Composer
    文件记录追加 file_put_content
    自定义导出表格
    异步处理接口 fsockopen
    appcache checking update
    js pix
    Event Aggregator
    ko list and css gradient
  • 原文地址:https://www.cnblogs.com/luxh/p/2501619.html
Copyright © 2020-2023  润新知