最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

I am doing an ajax call from my javascript to an aspx page's webmethod. The string I'm returning is wrapped in double-quotes for some reason. I tried stripping them out, but the replace only replaced the first one for some reason. There are no quotes wrapping the string on the server-side.

        var req = new XMLHttpRequest();
        var url = document.URL;
        // strip pound sign off the end
        var poundIndex = url.lastIndexOf('#');

        if (poundIndex === url.length - 1) {
            url = url.substring(0, poundIndex);
        }
        url += '/SignOn';
        req.open('post', url, false);
        req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
        req.send();

        var serverResponse = req.responseText.replace('"', '');

This is what I'm doing on the server:

Return System.Configuration.ConfigurationManager.AppSettings("url").ToString + "?token=" + HttpContext.Current.Session("Token").ToString() + "&aid=ca"

Any ideas?

I am doing an ajax call from my javascript to an aspx page's webmethod. The string I'm returning is wrapped in double-quotes for some reason. I tried stripping them out, but the replace only replaced the first one for some reason. There are no quotes wrapping the string on the server-side.

        var req = new XMLHttpRequest();
        var url = document.URL;
        // strip pound sign off the end
        var poundIndex = url.lastIndexOf('#');

        if (poundIndex === url.length - 1) {
            url = url.substring(0, poundIndex);
        }
        url += '/SignOn';
        req.open('post', url, false);
        req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
        req.send();

        var serverResponse = req.responseText.replace('"', '');

This is what I'm doing on the server:

Return System.Configuration.ConfigurationManager.AppSettings("url").ToString + "?token=" + HttpContext.Current.Session("Token").ToString() + "&aid=ca"

Any ideas?

Share Improve this question edited Apr 9, 2013 at 21:54 Yatrix asked Apr 9, 2013 at 21:41 YatrixYatrix 13.8k17 gold badges54 silver badges82 bronze badges 7
  • 2 The console automatically displays quotes around string values, so the string itself really only has single quotes. – Bart Commented Apr 9, 2013 at 21:49
  • should you not have System.Configuration.ConfigurationManager.AppSettings("url").ToString() + instead of System.Configuration.ConfigurationManager.AppSettings("url").ToString +? – Mark Schultheiss Commented Apr 9, 2013 at 21:50
  • @Bart those are nested, so the double quotes are included in the string. – Yatrix Commented Apr 9, 2013 at 21:52
  • @MarkSchultheiss it's VB.NET. It doesn't care as long as the parens are empty. – Yatrix Commented Apr 9, 2013 at 21:52
  • @Yatrix the developer console automatically wraps string values in quotes, so the outer quotes you see really aren't there. The inner quotes are supposed to be there, since JSON strings are always in quotes. See json.org -> string. Use JSON.parse(req.responseText); to parse the string. If you still want to get rid of all quotes, try req.responseText.replace(/\"/g, ""); – Bart Commented Apr 9, 2013 at 21:55
 |  Show 2 more comments

1 Answer 1

Reset to default 20

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

发布评论

评论列表(0)

  1. 暂无评论