2011-08-09 48 views
0

我正在製作一個JSP應用程序來調用對Servlet的Ajax請求。我參考了How to use servlets and Ajax如何通過Servlet從Ajax獲得HTML響應

我在加入responseText作爲我的div的innerHTML時遇到問題。我試過

$('#testdiv').html(responseText); 

但它顯示爲文本而不是HTML。

我的代碼是:-----------------

function getdata(){  
      var mobile = document.loyaltyForm.mobile.value; 
      var code = document.loyaltyForm.mobilecode.value; 
      var response = ''; 
      var testdiv = document.getElementById("testdiv"); 
      // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event... 
      $.post('loyal', {mob:mobile,mobilecode:code}, function(responseText) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text... 
       alert('Response:'+responseText); 
      }); 

    } 
+2

對不起,但你的問題是無法理解的。它是在HTML頁面上打印文本,因爲這就是你說的你想要的 - 你說你想在div中。我不明白這個問題。 – Ariel

+0

responseText在html中打印,但我想在div'testdiv'的innerHTML上打印responseText – Ankit

+0

我也試過:function(responseText){alert('hiii - >'+ responseText); response = responseText; } \t alert('hello - >'+ response);第一個警報是打印reponseText,但不會將值複製到響應變量。所以,第二個警報是隻打印你好..我希望我解釋我的評論.. – Ankit

回答

0

會發生當responseText實際上例如包含

<b>bold</b> <i>italic</i> 

而不是

<b>bold</b> <i>italic</i> 

換句話說,這會發生在您發送HTML轉義而不是真正的HTML字符串。確保您的servlet不發送HTML轉義的字符串。