如何将字符串中的反斜杠传递给jquery ajax调用?
在我的示例中 fileFolder 等于 \\ server \ media \
function getFileList() {
var fileFolder = $("#fileFolder").val(); // val equal to \\server\media\
$.ajax({
type: "POST",
url: "service/getFileList",
data: "{ 'folder': '" + fileFolder + "' }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
...do stuff here
}
}
});
}
答案 0 :(得分:1)
我认为数据应该是一个字符串,如下所示:
"folder=" + encodeURIComponent(fileFolder) + "&foo=bar"
或像这样的对象:
{folder: fileFolder}
jQuery将在发布数据之前进行转义。使用第二种方法,您的代码变为:
function getFileList() {
$.ajax({
type: "POST",
url: "service/getFileList",
data: {
folder: fileFolder
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// do stuff here
}
});
}
答案 1 :(得分:0)
将每个斜杠更改为双斜杠:
fileFolder = fileFolder.replace("\","\\");