2012-06-01 43 views
0

我有一個後端CGI腳本,通過Ajax調用返回我下面的文字中出現問題時:JS更換功能不按預期工作

<p>A problem occurred in a Python script. 
<p> /bin/cgi-bin/LOGS/tmpslM4pu.txt contains the description of this error. 

我試圖像下面顯示出來:

$.ajax({ 
      type: "POST", 
      url: "runcgi.py", 
      data: 
      { 
       'my_data' : 'test' 
      }, 
      success: function(html) 
      { 
       if(..test for success..) 
       { 
       } 
       else 
       { 
          var StrippedString = $(html).toString().replace(/(<([^>]+)>)/ig,"");                       var StrippedString = $(html).toString().replace(/(<([^>]+)>)/ig,""); 
          $("p").html(StrippedString); 

       } 
}); 

而下面是我的HTML代碼:

<body> 
     <p></p> 
</body> 

但我看到下面的輸出在我的瀏覽器:

[object Object] 

我該如何解決這個問題?

+0

使用console.log(html)在Firefox中使用Google Chrome或Firebug,然後在控制檯視圖中檢查元素。 – Christian

回答

3

您正在使用由html組成的jQuery對象並將其轉換爲字符串。結果是[object OBJECTTYPE],在這種情況下是[object Object]

取而代之,試試var StrippedString = html.replace(...);

+0

我曾嘗試過,但它聲明,html.replace不是一個函數,這就是爲什麼我添加toString()。使用FireBug我看到$(html)具有值'Document' – Prakash