2013-07-12 53 views
1

我米使用jQuery AJAX來獲取文本文件內容和那些需要在這裏的文本區域來顯示文本區域是我的前端代碼加載文本文件的內容爲

function getFileText() 
{ 
var fileName = "fileName.txt"; 
var data = { 
     "fileName" : fileName 
     } 

$.ajax({ 
    "dataType": 'json', 
    "type": "GET", 
    "url": "getFileText.htm", 
    "data":data , 
    cache:false, 
    success : function(resp){ 
     alert(resp); 
    }, 
    error : function(XMLHttpRequest, textStatus, errorThrown){ 
     alert("error"); 
    } 
}); 
} 

<textarea id="fileTextArea" rows="35" cols="250"> </textarea> 

這裏是我的服務器端代碼

public @ResponseBody String getFileText(HttpServletRequest request,HttpServletResponse response) throws IOException{ 
    String fileName = request.getParameter("fileName"); 
    String dirname="D:/java/"; 
    FileInputStream file = new FileInputStream(new File(dirname+fileName)); 
    DataInputStream in = new DataInputStream(file); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 
    StringBuffer str = new StringBuffer(); 
    //Read File Line By Line 
    while ((strLine = br.readLine()) != null) { 
     // Print the content on the console 
     str.append(strLine); 
     System.out.println(strLine); 

    } 

    //Close the input stream 
    in.close(); 
    return str.toString(); 

} 

我FILENAME.TXT什麼樣子

<html> 
<body> 
    <b> Hi user </b>, 
    <br> 
    <p>Welcome to FileText</p> 
<body> 
</html> 

現在,我可以能夠得到的迴應,但它是一個去ajax調用的錯誤塊,並顯示解析錯誤,...「意想不到的標記<」

請幫助我。如何正確地將文本文件內容放入響應中。

回答

1

你應該改變dataTypetexthtml

Datatype你告訴jQuery期待什麼樣的響應。所以在你的代碼中你期待json。

$.ajax({ 
    "dataType": 'json', 

發現數據類型和這裏的contentType ajax-datatype

+0

我將dataType json更改爲文本..現在它工作。非常感謝 – NivMack

0

<br>沒有結束標籤。使用<br/>

0

您還忘記關閉<body>標記。

<html> 
    <body> 
     <b> Hi user </b>, 
     <br /> 
     <p>Welcome to FileText</p> 
    </body> 
</html> 
-1

一個很好的解釋,我改變了

$.ajax({ 
"dataType": 'json', 

到 $阿賈克斯({ 「數據類型」: '文本',

現在正在工作..

謝謝..

相關問題