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.
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},
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With