AJax第一個小執行個體--初學者

來源:互聯網
上載者:User

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <script type="text/javascript" src="js/test.js"></script><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>   <body>  <form >    使用者名稱:<input type="text" id="name" /><br/>        <input type="submit" id="OK" value="校正" />     </form>  </body></html>function ajaxFunction() { var xmlHttp=null;  try    {   // Firefox, Opera 8.0+, Safari    xmlHttp=new XMLHttpRequest();    } catch (e)    {  // Internet Explorer   try      {      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }   catch (e)      {      try         {         xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");         }      catch (e)         {         alert("您的瀏覽器不支援AJAX!");         return false;         }      }    } return xmlHttp; }window.onload=function(){document.getElementById("OK").onclick=function(){//alert("wuuuu");/* * 1.擷取xmlHttpRequest * */var xmlReq=ajaxFunction();/* * 4.接手伺服器的響應 *  * *///alert("wuuuu");xmlReq.onreadystatechange=function(){if(xmlReq.readyState==4){alert(xmlReq.readyState);alert(xmlReq.status);if(xmlReq.status==200||xmlReq.status==304){alert("wu");var data=xmlReq.responseText;alert(data);}}}/* * 2.開啟伺服器的串連, * 第三個參數為true(非同步) * 第一個參數:傳遞方法 * 第二個方法是路徑 * *///alert("路徑整的不對啊???怎麼改過來???");xmlReq.open("get","/Ajax/Demo1",true);alert("adsada");/* * 3.發送資料 * */xmlReq.send(null);}}package cn.com.Demo;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.PageContext;public class Demo1 extends HttpServlet {/** *  */private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("text/html; charset=utf-8");PrintWriter out=response.getWriter();//out.write("OK");System.out.println("--------------------------------------");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}}


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.