2

I'm currently having the problem mentioned in the title and I'm somehow not finding a way to properly replace backsashes with double backslashes, so that I can properly give the string to a webservice as parameters. Let me show you what I tried. Some of these do actually work for some other people, but not for me... I'm currently testing this with FF18.0.1

WSParameters.replace(/\\/g, "\\\\\\\\");
WSParameters.replace("\\", "\\\\\\\\");
WSParameters.replace(/\\/g, "\\\\");
WSParameters.replace(/\\/g, "\\");
WSParameters.replace(/\\/g, "\");
WSParameters.replace("\\", "\\\\");

Thanks a lot in advance

EDIT: I should mention that it's somehow parsed into JSON and with firebug I see the backslash in the source string, but not in the JSON view. Maybe there is another way? But somehow it's already failing at the replacement of the backslashes.

EDIT2:

if (noAction == false) {
    $.ajax({
        type: "POST",
        url: "WebService.asmx/" + webMethod,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: pAsync,
        data: WSParameters,
        success: function callFunction(result) { processPOSTResults(result, pType, pNot);},
        error: function (xhr, ajaxOptions, thrownError) {
            alert('Error while communicating with WebAdmin web service. - ' + xhr.status + " " + thrownError);
        }
    });
}
4
  • Why not this : WSParameters.replace('\', '\\ ' Commented Jan 31, 2013 at 11:17
  • Because this will escape the ', so the string definition is broken. Commented Jan 31, 2013 at 11:19
  • 3
    Watch out with just relying on Firebug's output. When you log "\\" (a backslash) to the console, it prints as "\". Similarly, "\"" (a quote) is output as """. By the looks of it, the third example should work just fine. Commented Jan 31, 2013 at 11:39
  • Thanks Mattias, however I temporarily wrote my own replace method and fell into another problem. Now it seems that I can't provide the backslash in any way to th web service in JSON. \\\\, %5C, \u05C and \ don't seem to be acceptable. Commented Jan 31, 2013 at 12:03

1 Answer 1

7

WSParameters.replace(/\\/g, "\\\\"); should do it, and in FF18 as well. Notice that if you use JSON.stringify, this is done automatically. Also watch out that many console outputs (Firebug etc) do surround string contents with quotes, but do not escape them.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks Bergi, I don't use JSON stringify, please se my EDIT 2 for the JSON WS part.
However maybe you should use it for posting JSON? What is WSParameters, how do you get it?
The WSParameters variable is a string and looks like this: {param1: 'abc', param2: 'def', list1: ['4711','4712']}
I'll have a look at this, thanks. But is this also possible with dynamically generated lists and nested lists?
Yes. Use an object, and JSON.stringify (like here).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.