2011-06-09 53 views
0

我無法通過servlet響應來獲取Json文本。servlet代碼正在工作。我的Ajax代碼有問題。代碼...通過ajax檢索Json文本的問題

var json = eval('(' + xmlhttp.responseText +')'); 

...沒有返回任何東西。有沒有需要這樣做的罐子?以下是我的代碼:

//Servlet 

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { 
    List result = new ArrayList(); 
    result.add(new SearchResponse("001", "User Manual", "Operator")); 
    response.setContentType("application/json");   
    response.setCharacterEncoding("UTF-8");   
    response.getWriter().write(new Gson().toJson(result)); 
} 

在我的Ajax中,我正在編寫下面的代碼來獲取它。

function ajaxFunction() { 
    alert("function called..."); 
    if (xmlhttp) { 
     alert(AJAX_SERVLET); 
     xmlhttp.open("GET", AJAX_SERVLET, true); //AJAX_SERVLET has the servlet path 
     xmlhttp.onreadystatechange = handleServerResponse; 
     xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
     xmlhttp.send(null); 
    } 
} 

function handleServerResponse() { 
    alert(xmlhttp.readyState); 
    if (xmlhttp.readyState == 4) { 
     alert(xmlhttp.status); 
     if (xmlhttp.status == 200) { 
      alert(xmlhttp.responseText); 
      alert(json); 
      var json = eval('(' + xmlhttp.responseText +')'); 
      request.setAttribute("output",json); 
     } else { 
      alert("Error during AJAX call. Please try again"); 
     } 
    } 
} 
+0

如果你只是使用'的eval(xmlhttp.responseText)會發生什麼'甚至跳過EVAL部分?你有什麼數據嗎? – psema4 2011-06-09 16:32:47

+0

你可以發佈你的'responseText',這樣我們可以看到它是否有效?您的警報中哪些 - 如果有的話 - 觸發? – ajm 2011-06-09 16:46:55

回答

0

我不能與JSP幫助,但希望這個例子將有助於對JavaScript的部分:

<script> 
... 
function handleServerResponse() { 
    if (xmlhttp.readyState == 4) { 
     if (xmlhttp.status == 200) { 
      var json = JSON.parse(xmlhttp.responseText); 

      // json now contains an array of objects, eg: 
      // 
      // [ 
      // { 
      //  "productNumber" : "001", 
      //  "productType" : "User Manual", 
      //  "funcDesignation": "Operator" 
      // } 
      // ] 

      // grab the first (and only) object in the array 
      var firstRecord = json[0]; 

      // update the UI by selecting DOM elements and rewriting their contents 
      document.getElementById('product-number').innerHTML = 
       firstRecord.productNumber; 

      document.getElementById('product-type').innerHTML = 
       firstRecord.productType; 

      document.getElementById('func-designation').innerHTML = 
       firstRecord.funcDesignation; 

     } else { 
      alert("Error during AJAX call. Please try again"); 
     } 
    } 
} 
</script> 
... 
<h4>Product</h4> 
<div>Number: <span id="product-number"></span></div> 
<div>Type: <span id="product-type"></span></div> 
<div>Function: <span id="func-designation"></span></div> 
... 

PS - 你可能要考慮的jQuery,MooTools的或其他現代JavaScript框架;他們使AJAX調用和DOM的工作更容易。

+0

您正在測試哪個瀏覽器?儘管JSON.parse在現代瀏覽器中幾乎是標準的,但您可能需要通過[小腳本]添加它(http://stackoverflow.com/questions/4908875/is-json-parse-supported-by-all-major-瀏覽器)適用於舊版瀏覽器。 – psema4 2011-06-14 11:52:26

1

我有同樣的問題,但我改變了啓動部分&在我的情況下它的工作。

阻止代碼:

 var json = eval('(' + xmlhttp.responseText +')'); 

工作代碼:

var json = ""; 
    json = eval('(' + xmlhttp.responseText +')');  

問候 希亞姆Miyatra