這篇文章將為大家詳細(xì)講解有關(guān)ajax怎么動(dòng)態(tài)加載json數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)基于10余年網(wǎng)站建設(shè)行業(yè)經(jīng)驗(yàn),一直致力于為中小型企業(yè)提供信息化解決方案,創(chuàng)新互聯(lián)做到開(kāi)放、開(kāi)源,讓企業(yè)所有核心數(shù)據(jù)掌握到企業(yè)手里,打破行業(yè)亂象,讓企業(yè)被網(wǎng)絡(luò)公司掌控的局面不再發(fā)生;美工設(shè)計(jì)部,產(chǎn)品/程序研發(fā)部,營(yíng)銷(xiāo)策劃部,售后客服部。一切服務(wù)為企業(yè)量身定制,專(zhuān)注為您!
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代碼
//get public 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 = "測(cè)試名字admin"; } if (age == null || age == "") { age = "測(cè)試年齡100"; } user user = new user(1, name, age); PrintWriter out = response.getWriter(); JSONObject jsonObj = JSONObject.fromObject(user); out.print(jsonObj); out.flush(); out.close(); }
//post public 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 = "測(cè)試名字admin"; } String age = request.getParameter("age"); if (age == null || age == "") { age = "測(cè)試年齡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>
關(guān)于ajax怎么動(dòng)態(tài)加載json數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
新聞標(biāo)題:ajax怎么動(dòng)態(tài)加載json數(shù)據(jù)
轉(zhuǎn)載源于:http://jinyejixie.com/article36/ggsppg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、品牌網(wǎng)站制作、軟件開(kāi)發(fā)、微信公眾號(hào)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)