Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Read Value with Backslashes in json or javascript or jquery

I need to transfer value/s (Windows path) to json using jQuery Ajax so that the value will be thrown or decoded to PHP script, but it can't read value/s with backslashes in json. It must be transferred to json value with the whole path with backslashes in it.

My Sample Codes:

/*==========================================================================*/


var file_name = "C:\WINDOWS\Temp\phpABD.tmp";

var jsonSearchContent = "{\"file_name\":\""+file_name+"\"}";


            $.ajax({
                type:"POST",
                dataType: "html",
                url: url,
                data: {sendValue:jsonSearchContent},
                complete: function (upload) {
                    alert(upload.responseText);
                }
            }
            );
/*==========================================================================*/

Thanks in advance.

like image 921
Kompyuter Endyinir Avatar asked Feb 19 '23 22:02

Kompyuter Endyinir


2 Answers

Escape it.

var file_name = "C:\\WINDOWS\\Temp\\phpABD.tmp";

By the way, you don't need to use json format to send to php, just send the value directly and not necessary to do json_decode in php side.

data: {file_name: file_name},
like image 101
xdazz Avatar answered Feb 21 '23 12:02

xdazz


The backslash character in javascript is used to escape special characters like tabs, carriage returns, etc. In a javascript string, if you want to represent an actual backslash character, use '\\' and it will be treated as a single backslash. Try this:

$.ajax({
    type:"POST",
    dataType: "html",
    url: url,
    data: {
        sendValue: {
            file_name: "C:\\WINDOWS\\Temp\\phpABD.tmp"
        }
    },
    complete: function (upload) {
        alert(upload.responseText);
    }
});

Here's the w3schools page on javascript strings.

like image 38
Dan Avatar answered Feb 21 '23 12:02

Dan