How to read value using backslashes in json or javascript or jquery

I need to pass the / s value (Windows path) to json using jQuery Ajax so that the value is output or decoded in a PHP script, but it cannot read / with backslashes in json. It should be ported to json value with the whole path with backslashes in it.

My code examples:

/*==========================================================================*/ 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.

+4
source share
2 answers

The escape.

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

By the way, you do not need to use the json format to send to php, just send this value directly and do not need to do json_decode on the php side.

 data: {file_name: file_name}, 
+3
source

The backslash character in javascript is used to call special characters such as tabs, carriage returns, etc. In the javascript line, if you want to represent the actual backslash character, use '\\' and it will be treated as one backslash. Try the following:

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

+2
source

Source: https://habr.com/ru/post/1438335/


All Articles