斜杠从jquery ajax发布数据中删除

时间:2013-05-22 13:12:16

标签: asp.net json jquery

$.ajax({
                type: "POST",
                url: "DataImport.aspx/InsertInitialFile",
                contentType: "application/json; charset=utf-8",
                processData: false,
                data: '{"companyId":"' + "1" + '","importFileName":"' + "license.txt" + '","filePath":"' + "UploadFolder\Initial\Temprahullate" + '","importStatusId":"' + "5" + '"}',

                //data: '{"companyId":"' + companyId + '","importFileName":"' + importFileName + '","filePath":"' + filePath + '","importStatusId":"' + importStatusId + '"}',
                dataType: "json",

                success: function (Msg) {
                    $("#<%= upload_Initialfilename.ClientID %>").val("");
                    $("#<%= ddlCompany.ClientID %>").val("0");
                    $('#<%= lblMessage.ClientID %>').html("File saved successfully.");
                    $("#<%= lblMessage.ClientID %>").css("color", "Green");
                },
                error: function (Msg) {
                    console.log(Msg);
                    alert('error');
                }
            });

我正在尝试使用jquery ajax来调用web方法。它工作正常,当我手动静态发布数据时,如上面的代码

 data: '{"companyId":"' + "1" + '","importFileName":"' + "license.txt" + '","filePath":"' + "UploadFolder\Initial\Temprahullate" + '","importStatusId":"' + "5" + '"}',

但真正的问题是,当我在服务器端检查filePath字段的数据时,它会删除其中的斜杠。意味着代替 UploadFolder\Initial\Temprahullate ,我在服务器端方法中得到 UploadFolderInitialTemprahullate

我需要那些斜杠,因为那是文件夹的路径。

1 个答案:

答案 0 :(得分:1)

Javascript中的\字符是转义字符。要在您的字符串中添加一个\,您实际上需要使用\\。像这样:

$.ajax({
            type: "POST",
            url: "DataImport.aspx/InsertInitialFile",
            contentType: "application/json; charset=utf-8",
            processData: false,
            data: '{"companyId":"' + "1" + '","importFileName":"' + "license.txt" + '","filePath":"' + "UploadFolder\\Initial\\Temprahullate" + '","importStatusId":"' + "5" + '"}',

            //data: '{"companyId":"' + companyId + '","importFileName":"' + importFileName + '","filePath":"' + filePath + '","importStatusId":"' + importStatusId + '"}',
            dataType: "json",

            success: function (Msg) {
                $("#<%= upload_Initialfilename.ClientID %>").val("");
                $("#<%= ddlCompany.ClientID %>").val("0");
                $('#<%= lblMessage.ClientID %>').html("File saved successfully.");
                $("#<%= lblMessage.ClientID %>").css("color", "Green");
            },
            error: function (Msg) {
                console.log(Msg);
                alert('error');
            }
        });