Ajax在JQuery的運用(load方法練習)

來源:互聯網
上載者:User

  1.index.html

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>test load()</title><script type="text/javascript" src="script/jquery-3.1.1.js"></script><script type="text/javascript">  $(function() {    $("input:eq(0)").click(function() {        $("#content").load("LoadServlet");        //$("#content").load("LoadServlet #myli");//過濾篩選    });        $("input:eq(1)").click(function() {        $("#content").load("LoadServlet",{'username':'Haige','age':30},function(responseText,textStatus,XMLHttpRequest){         // alert(textStatus);//擷取服務端傳回資料的狀態         // alert(responseText);//服務端傳回的文本資料         // alert(XMLHttpRequest.status);//服務端傳回非同步處理對象          alert(XMLHttpRequest。responseText);        });//過濾篩選    });    });</script></head><body>  <input type="button" value="載入get方法"/>  <input type="button" value="載入Post方法"/>  <div id="content"></div></body></html>

LoadServlet.java

package com.haige;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/LoadServlet")public class LoadServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    public LoadServlet() {        super();        // TODO Auto-generated constructor stub    }    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub     response.setContentType("text/html;charset=utf-8");     response.setCharacterEncoding("utf-8");     PrintWriter out =response.getWriter();     out.println("<ul id='myul'>");     out.println("<li id='myli'>北京</li>");     out.println("<li>上海</li>");     out.println("<li>南京</li>");     out.println("</ul>");     out.close();    }    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        request.setCharacterEncoding("utf-8");        String username=request.getParameter("username");        int age=Integer.parseInt(request.getParameter("age"));        response.setCharacterEncoding("utf-8");        PrintWriter out =response.getWriter();        out.println("<p>我的名字叫:"+username+",今年"+age+"歲</p>");        out.close();    }}

截圖:


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.