jquery 的ajax应用

来源:互联网 发布:caffe python用的是a 编辑:程序博客网 时间:2024/04/29 14:53

html页面:


  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>


  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8.   <head>
  9.     <base href="<%=basePath%>">
  10.     
  11.     <title>My JSP 'form_test.jsp' starting page</title>
  12.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  13. <meta http-equiv="pragma" content="no-cache">
  14. <meta http-equiv="cache-control" content="no-cache">
  15. <meta http-equiv="expires" content="0">    
  16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  17. <meta http-equiv="description" content="This is my page">
  18. <!--
  19. <link rel="stylesheet" type="text/css" href="styles.css">
  20. -->
  21. <!-- 导入 jQuery 库 -->
  22.      <script type="text/javascript" src="jquery-1.7.2.js"></script>
  23. <script type="text/javascript">
  24.        $(document).ready(function(){
  25.    $("#sedd_ajax").click(function(){
  26.     var params = $("input").serialize();//序列化表单的值key=value形式
  27.     alert(params);
  28. $.ajax(
  29. {
  30.    url:"Ajaxservlet",                //发送到后台处理的地址
  31. type:'post',                  //提交方式
  32. data:params,                 //传递的参数
  33. dataType:'json',            //接受数据格式
  34. success:function(data){
  35. },   
  36. error : function(XMLHttpRequest, textStatus, errorThrown) {   
  37.                            alert(XMLHttpRequest.status);  //200
  38.                            alert(XMLHttpRequest.readyState);  //4
  39.                            alert(textStatus);//解析文本状态
  40.                            alert('读取超时,请检查网络连接');
  41.                         }
  42. }
  43. );
  44. });

  45. //$.post提交方式
  46. /* $("#test_post").click(function(){
  47.    $.post(                              //该方法接收  参数,url 参数
  48. 'ajax_test.jsp',
  49. {
  50.   username:$("#input1").val(),
  51.   userage:$("#input2").val(),
  52.   usersex:$("input3").val(),
  53.   userjob:$("input4").val()
  54. },
  55. function(data){                    //这个是回调函数
  56.   var myjson;
  57.   eval('myjson='+data+';');
  58.   $("#result").html("姓名="+myjson.username+"<br/>工作"+myjson['userjob']);
  59. }
  60. );
  61. }); */
  62. });
  63.    
  64.   </script>


  65.   </head>
  66.   
  67.   <body>
  68.     <body topmargin="0" marginwidth="0" marginheight="0">
  69.        <div id="result" style="background:orange;border:1px solid red;width=300px;height=600px;"></div>
  70.        <form id="formtest" action="">
  71.         <p><span>姓名</span><input type="text" name="username" id="input1" value=""/></p>
  72.        <p><span>年龄</span><input type="text" name="userage" id="input2" value=""/></p> 
  73.        <p><span>性别</span><input type="text" name="usersex" id="input3" value=""/></p>
  74.       <p><span>职业</span><input type="text" name="userjob" id="input4" value=""/></p>
  75.        </form>
  76.      <button id="sedd_ajax">提交</button>
  77.      <button id="test_post">post提交</button>
  78.      <button id="test_get">get提交</button>
  79.     </body>
  80.   </body>
  81. </html>



  1. Java页面:

  2. public void doPost(HttpServletRequest request, HttpServletResponse response)
  3. throws ServletException, IOException {
  4.               System.out.println(request.getParameter("username"));
  5. response.setContentType("text/html");
  6. PrintWriter out = response.getWriter();
  7. ArrayList<User> list=new ArrayList<User>();
  8.          User user = new User(); 
  9.          user.setId(1);
  10.          user.setAge(20);
  11.          user.setName("wkl");
  12.          //转为json对象
  13.          list.add(user);
  14.          //将List转化为JSON
  15.          JSONArray json=JSONArray.fromObject(list);
  16.          System.out.println(json);
  17.          out.write(json.toString());
  18. out.flush();
  19. out.close();
  20. }

0 0
原创粉丝点击