2014-02-27 75 views
0

我的jsp代碼中有兩個ajax調用去了servlet。在第一次調用中,我在會話中設置一個值,在另一次調用中,我從會話中檢索相同的值。現在這個值作爲ajax調用的響應(第2個Ajax調用)。 我的問題是: - 該值包含「\ n」(例如 - (「ABC \ n def \ n geh \ n xyz」))。當我將此值存儲在js變量中並嘗試訪問它時,僅需要字符串「\ n」。它不是在JSP其識別爲換行符JavaScript變量不識別換行符

AJAX調用: -

$.ajax({ 
    type : "POST", 
    url : "ConfiguratorStatusReportServlet?method=datagrid", 
    data : finaldata, 
    datatype : "JSON", 
    async : false, 
    success : function(msg) {  
     $('#contentDiv').show();   
     fillDataGrid(msg);  
    } 
}); 


$.ajax({ 
    type : "POST", 
    url : "ConfiguratorStatusReportServlet?method=chart", 
    data : finaldata, 
    datatype : "JSON", 
    async : false, 
    success : function(msg) { 
     fillDataChartData(msg); 
    } 
}); 

代碼中的servlet: -

HttpSession session = request.getSession(); 
String method = request.getParameter("method"); 
if(method.equalsIgnoreCase("datagrid")) 
{ 
    JSONArray listjson = CSR.firstcalledMethod(); 

    String chartformat = CSR.callingMethod2(); 
    System.out.println("chartformat in servlet = "+chartformat); 

    String result = listjson.toString(); 
    String checkDataExists = (String) (session.getAttribute("chartformat") == null ? "Invalid" : session.getAttribute("chartformat")); 
    if(!checkDataExists.equalsIgnoreCase("Invalid")) 
    { 
     session.removeAttribute("chartformat"); 
    }   
    session.setAttribute("chartformat", chartformat); 
    out.write(result); 
} 
else 
{ 
    String chartResult = (String) session.getAttribute("chartformat"); 
    session.removeAttribute("chartformat"); 
    out.write(chartResult); 
} 
現在

在包含上面顯示的AJAX調用相同的jsp我試圖訪問該變量: -

function fillDataChartData(dataVAR) { 
    var chartdata = dataVAR;     
    alert("chartdata = "+chartdata); 
} 

假設ajax中的響應包含「APAC-OTHER,0.05 \ n FCS,99.95」(即, dataVAR =「ABC \ n DEF \ n GHI」)。現在,當我試圖在功能fillDataChartData(dataVAR)提醒它,它顯示「亞太地區,其他,0.05 \ n FCS,99.95」的警報,但我希望它像亞太地區,其他,0.05 FCS,99.95

我該怎麼做?請幫忙...

回答

0

這很奇怪。在你的迴應中可能有一些隱藏的字符?無論如何,你可以嘗試用br標籤代替換行符:

function fillDataChartData(dataVAR) { 
    var chartdata = dataVAR.replace(/\\n/gm, '<br>');     
    alert("chartdata = "+chartdata); 
}