不能通过eval评估Json响应

时间:2012-03-10 18:05:57

标签: ajax json

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

这里的问题'var myObject = eval('('+ text +')');'无法评估来自服务器的json返回响应。(结果就在上面)。 我的问题。为什么结果包含这些额外的标记。

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


提前致谢。

1 个答案:

答案 0 :(得分:0)

除了代码之外,JSP还包含文字HTML。您只需要<%@page import...%><% ... %>中的中心代码来完成工作。