簡體   English   中英

在 Java 中表示路徑的字符串以雙反斜杠返回到 ajax 調用

[英]String representing a path in Java is returned with double backslash to an ajax call

我正在嘗試將字符串從 Java 返回到 ajax 調用。 該字符串包含應在瀏覽器中打開的 excel 文件的本地路徑:例如:“D:\\docs\\file.xls” 問題是返回時返回雙反斜杠,如下所示:“D:\\docs \\file.xls" 我嘗試在 Java 和 javascript 中使用正則表達式來糾正這個問題,但路徑仍然與雙反斜杠保持不變。

阿賈克斯調用

$.ajax({
        url : baseUrl + "autotest/process",
        type : 'GET',
        data : '',
        dataType : 'text',
        contentType: 'text/plain',
        processData: false,
        success : function(data) {
            //Here data contain a path with double backslash \\
            //even if i try to strip out the \\ as follow:
            var str = data.replace(/\\/g, "\\");
           //str still have the double backslash \\

Java中的控制器:

@RequestMapping(value = "app/process", method = RequestMethod.GET)
    public @ResponseBody String process(HttpServletRequest request, HttpServletResponse response) {
    // code...

    // trying to strip all the double backslash
    String path = str.replaceAll("\\\\\\\\", "\\\\");
    // path still have the double backslash \\
    return path;
}

我怎么解決這個問題?

非常感謝

如果您的目標是從data刪除雙反斜杠\\\\ ,那么您在 ajax 調用中使用的正則表達式不正確。
它應該是/\\\\\\\\/g而不是/\\\\/g

var str = data.replace(/\\\\/g, "\\");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM