繁体   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