2012-03-10 46 views
0
**index.jsp (client side)** 
************************************************************************************** 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Techniques AJAX - XMLHttpRequest</title> 
     <script type="text/javascript" src="oXHR.js"></script> 
     <script type="text/javascript"> 
      <!-- 
      function request(callback) { 

       var xhr = getXMLHttpRequest(); 

       xhr.onreadystatechange = function(){ 
        if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) { 

         alert(xhr.responseText); 

         var text = xhr.responseText; 

         var myObject = eval('(' + text + ')'); 

         callback(myObject.Addresses); 


        } 
       }; 

       xhr.open("GET", "jsonResponse.jsp", true); 
       xhr.send(null); 
      } 

      function readData(oData) { 
       alert(oData); 

      } 


      function getXMLHttpRequest() { 
       var xhr = null; 

       if (window.XMLHttpRequest || window.ActiveXObject) { 
        if (window.ActiveXObject) { 
         try { 
          xhr = new ActiveXObject("Msxml2.XMLHTTP"); 
         } catch(e) { 
          xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
         } 
        } else { 
         xhr = new XMLHttpRequest(); 
        } 
       } else { 
        alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest..."); 
        return null; 
       } 

       return xhr; 
      } 

      //--> 
     </script> 
    </head> 
    <body> 
     <p> 
      <button onclick="request(readData);">Afficher le fichier XML</button> 
      <div id="output"></div> 
     </p> 
    </body> 
</html> 

***jsonResponse.jsp (server side)*** 
********************************************************************************** 
<%-- 
    Document : jsonResponse 
    Created on : Mar 4, 2012, 8:39:23 PM 
    Author  : ghorbel 
--%> 

<%@page import="org.json.JSONException"%> 
<%@page import="org.json.JSONArray"%> 
<%@page contentType="text/html; charset=UTF-8"%> 
<%@page import="org.json.simple.JSONObject"%> 

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Hello World!</h1> 
     <% 
     JSONObject json  = new JSONObject(); 
     JSONArray addresses = new JSONArray(); 
     JSONObject address; 
     try 
     { 
      int count = 15; 

      for (int i=0 ; i<1 ; i++) 
      { 
      address = new JSONObject(); 
      address.put("CustomerName"  , "Decepticons" + i); 

      addresses.put(i,address); 
    } 
     json.put("Addresses", addresses); 
} 
catch (JSONException jse) 
{ 

} 
response.getWriter().flush(); 

response.setContentType("application/json"); 

response.getWriter().write(json.toString()); 

     %> 
    </body> 
</html> 



The result of 'alert(xhr.responseText);' (the json return response from the server). 
************************************************************************************************* 
{"Addresses":[{"CustomerName":"Decepticons0"}]} 






<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <h1>Hello World!</h1> 

    </body> 
</html> 
**************************************************************************************** 

這裏的問題是, '變種myObject的=的eval('( '+文本+'));' 來評價無法評估來自服務器的json返回響應(結果如上)。 我的問題。爲什麼結果包含這些額外的內存。JSON響應不能不受EVAL

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


    </body> 
</html> 

當它應該只返回 { 「地址」:[{ 「客戶名稱」: 「Decepticons0」}]}。

如果有人能告訴我程序有什麼問題。 我使用Netbeans,Glassfish。


在此先感謝。

回答

0

和您的代碼一樣,您的JSP包含文字HTML。你只需要<%@page import...%>和你的中心代碼<% ... %>來完成這項工作。

+0

謝謝詹姆斯。沒錯,你對 – 2012-03-10 19:02:13

+0

謝謝詹姆斯。確實,你是對的 – 2012-03-10 19:03:21