2012-01-18 28 views
9

JSP ile AJAX'ı öğrenmeye çalışıyorum ve aşağıdaki kodu yazdım. Bu çalışıyor gibi görünmüyor. Lütfen yardım:JSP'li Basit bir AJAX örneği

Bu benim configuration_page.jsp olan bu yukarıda AJAX kodundan erişmeye çalışıyorum benim get_configuration.jsp olduğunu

<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> 

: Ben jQuery AJAX kullandık

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

    </head> 
    <body> 

    <h4>Mee..</h4> 
    </body> 
</html> 
+0

Eğer olmadan çalışır, o zaman ',' ',' '' içermemelidir . Sadece

'içermelidir. Kendinize bir iyilik yapın ve jQuery'yi kullanın. –

+0

Ayrıca jQuery kullanmanız önerilir: daha basit bir sözdizimi ve crossbrowser taşınabilirliği vardır. – Dims

cevap

1

"configuration page.jsp" dosyasında hata yapıyorsunuz. Bu dosyada burada , fonksiyon loadXMLDoc() 'in hat numarası 2 böyle olmalı:

var config=document.getElementsByName('configselect').value; 

Eğer <select> etiketinde sadece name özelliğini ilan ettiler çünkü. Yani bu elementi isme göre almalısın.

bu düzelttikten sonra, bu get_configuration.jsp sonucu bir div içeriği olarak kullanılan herhangi bir JavaScript hatası

12

AJAX isteklerini yap.

Kontrol Aşağıdaki kod:

<html> 
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('#call').click(function() 
      { 
       $.ajax({ 
        type: "post", 
        url: "testme", //this is my servlet 
        data: "input=" +$('#ip').val()+"&output="+$('#op').val(), 
        success: function(msg){  
          $('#output').append(msg); 
        } 
       }); 
      }); 

     }); 
    </script> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body> 
    input:<input id="ip" type="text" name="" value="" /><br></br> 
    output:<input id="op" type="text" name="" value="" /><br></br> 
    <input type="button" value="Call Servlet" name="Call Servlet" id="call"/> 
    <div id="output"></div> 
</body> 

0

loadXMLDoc JS işlevi aksi takdirde postback neden olacaktır, yanlış dönmelidir.

İlgili konular