国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

ajax動態(tài)加載json數(shù)據(jù)并詳細(xì)解析

2024-09-01 08:31:10
字體:
供稿:網(wǎng)友

效果圖

jsp代碼

<form >   姓名:<input name="name" type="text"/>   年齡:<input name="age" type="text"/>   <input type="button" class="get" value="get提交"/>   <input type="button" class="post" value="post提交"/>   <input type="button" class="ajax" value="ajax提交"/>   </form>   <div id="box"></div>

servlet代碼

//getpublic void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  response.setContentType("text/html");  response.setCharacterEncoding("utf-8");  response.setContentType("text/html;charset=utf-8");  String name = request.getParameter("name");  String age = request.getParameter("age");  if (name == null || name == "") {   name = "測試名字admin";  }  if (age == null || age == "") {   age = "測試年齡100";  }  user user = new user(1, name, age);  PrintWriter out = response.getWriter();  JSONObject jsonObj = JSONObject.fromObject(user);  out.print(jsonObj);  out.flush();  out.close(); }
//postpublic void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  // response.setContentType("text/html");  request.setCharacterEncoding("utf-8");  response.setCharacterEncoding("utf-8");  response.setContentType("text/html;charset=utf-8");  String name = request.getParameter("name");  if (name == null || name == "") {   name = "測試名字admin";  }  String age = request.getParameter("age");  if (age == null || age == "") {   age = "測試年齡100";  }  user user = new user(1, name, age);  PrintWriter out = response.getWriter();  JSONObject jsonObj = JSONObject.fromObject(user);  out.print(jsonObj);  out.flush();  out.close(); }

JS核心代碼

<script type="text/javascript">  //get $(document).ready(function() {  $('form .get').click(function() {   $.get('ajaxServlet',function(response,status,xhr){    var dataObj = eval("(" + response + ")");    $("#box").html(response);    alert(dataObj.name);  }); }); //post $('form .post').click(function() {   $.post('ajaxServlet',function(response,status,xhr){    var dataObj = eval("(" + response + ")");    $("#box").html(response);  }); }); //ajax $('form .ajax').click(function() {  alert($("[name='name']").val());    $.ajax({    type:'get',    url:'ajaxServlet',    data:{    name:$("[name='name']").val(),    age:$("[name='age']").val()    },    success:function(response, status, xhr){    $("#box").html(response);}    }); }); });</script>

以上這篇ajax動態(tài)加載json數(shù)據(jù)并詳細(xì)解析就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 锡林郭勒盟| 兴化市| 潼关县| 天柱县| 兰考县| 萨迦县| 印江| 神池县| 嵊州市| 郑州市| 凤凰县| 本溪| 平定县| 特克斯县| 浙江省| 府谷县| 合江县| 赣州市| 满城县| 香格里拉县| 文登市| 文成县| 湘西| 阳江市| 阜城县| 廉江市| 行唐县| 河南省| 德令哈市| 浏阳市| 乐都县| 淮安市| 文登市| 平原县| 沅陵县| 梧州市| 广丰县| 西平县| 宜宾市| 新安县| 闻喜县|