Why is my server code ajax call returning a response wrapped in double-quotes? Why is my server code ajax call returning a response wrapped in double-quotes? asp.net asp.net

Why is my server code ajax call returning a response wrapped in double-quotes?


Firstly, if you're concerned that you see ""mystring"" instead of "mystring" (double quotes instead of single quotes), that's because the developer console automatically displays quotes around string values, which can be confusing if your string actually contains the "quote" character. The outer quotes you see in the console aren't there, only the inner quotes are.

Next, according to the JSON spec (http://www.json.org/) JSON strings start and end with quotes. If you wish to parse json strings, use:

var str = JSON.parse(req.responseText);

If you simply wish to get rid of all quotes in a string, try

var str = req.responseText.replace(/\"/g, "");

Note that the latter gets rid of ALL quotes, including (escaped) quotes in the middle of the string.

If you're working with JSON objects, as your response header (application/json) seems to indicate, I strongly recommend working with JSON.parse