简体   繁体   中英

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

i'm trying to return a string from Java to an ajax call. The string contains the local path for an excel file that should be opened in the browser: ex: "D:\\docs\\file.xls" The problem is that when in returned is returned with double backslash as follow: "D:\\docs\\file.xls" I tried to use regex, both in Java and in javascript to correct this, but the path still remain the same with the double backslash.

The ajax call

$.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 \\

The controller in 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;
}

How can i solve this problem?

Thank you very much

If your goal is to remove double back-slashes \\\\ from data , then the regex you're using in the ajax call isn't correct.
It should be /\\\\\\\\/g instead of /\\\\/g :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM