Eine Einfache AJAX mit JSP-Beispiel

Ich versuche zu lernen, mit AJAX JSP und ich geschrieben haben, den folgenden code. Dies scheint nicht zu funktionieren. Bitte helfen:

Dies ist mein configuration_page.jsp

    <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>JSP Page</title>
     <script type="text/javascript">

      function loadXMLDoc()
      {
        var xmlhttp;
        var config=document.getElementById('configselect').value;
        var url="get_configuration.jsp";
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
        }

        xmlhttp.open("GET", url, true);
        xmlhttp.send();
}
</script>        

</head>

<body>
  <h2 align="center">Saved Configurations</h2>
   Choose a configuration to run: 
  <select name="configselect" width="10">
    <option selected value="select">select</option>
    <option value="Config1">config1</option>
    <option value="Config2">config2</option>
    <option value="Config3">config3</option>
  </select> 
  <button type="button" onclick='loadXMLDoc()'> Submit </button>
  <div id="myDiv">
    <h4>Get data here</h4>
  </div>
 </body>
</html>

Dies ist mein get_configuration.jsp, die ich versuche, Zugang von der AJAX-code oben:

<html>
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>JSP Page</title>

  </head>
  <body>

    <h4>Mee..</h4> 
  </body>
</html>
  • Wenn das Ergebnis der get_configuration.jsp wird als der Inhalt von einem div, dann sollte es nicht enthalten <html>, <head>, <body>. Es sollten nur die <h4>. Tun Sie sich einen gefallen und verwenden Sie jQuery.
  • Empfehle auch jQuery verwenden: es hat eine einfachere syntax und crossbrowser Portabilität.
InformationsquelleAutor user_abh | 2012-01-18
Schreibe einen Kommentar