none
escape characters not being preserve in json after ajax call RRS feed

  • Question

  • Hello,

    I have a web API that returns data to a web page in json format. One of the items in the json is: "Title":"blah blah blah". One title I encountered contains a quote being used for inches: "1" manual valve on group production..." When the API generates the json string, it adds all the necessary escape characters, and as far as I can tell, it looks good:

    \"Title\":\"1\\\" manual valve on group production...\"

    But then when it is retrieved by the javascript, the extra slash is taken away:

    "Title":"1" manual valve on group production..."

    I would expect at least one slash:

    "Title":"1\" manual valve on group production..."

    Needless to say it's screwing up the parsing of the json.

    Here is my API code:

    [code]
    [Authorize]

    public async Task<HttpResponseMessage> GetTop3ReportCard([FromUri] int projectId, [FromUri] string othersIdCsv = null)
           {
                
    if (projectId <= 0)
    {
    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
     "Invalid project ID.");
    }

    //... 

    Top3AndReportCard top3AndReportCard = GetTop3AndReportCard();

    string jsonString = JsonConvert.SerializeObject(top3AndReportCard);


    return Request.CreateResponse(HttpStatusCode.OK, jsonString);
    }
    [/code]

    And here's the javascript that calls it:

    [code]
                var projectId = store.get('filterProjectId');
                $.get('ReportConfig/GetJSONString', { projectID: projectId })

    .done(function (data) {
                        ra.reportConfigPage.ReportJsonParsed = JSON.parse(data); // <-- Offending line
                        ra.reportConfigPage.ReportJsonParsedOriginal = JSON.parse(data);
                        ra.reportConfigPage.InitCommentBoxes();
                    })

    .fail(function (jqXhr, textStatus, errorThrown) {
                        alert('There was an error retrieving the JSON String:\n' +

    'jqXhr = ' + jqXhr + ', textStatus = ' + textStatus + ', errorThrown = ' + errorThrown);
                        $("#spinning-loader").hide();
                        $("#overlay").hide();
                    });
    [/code]

    It gets to the done() function in the ajax call but then crashes at the line marked "offending line".

    Can anyone suggest why the slash is not being preserved when it returns from the AJAX call?
    • Moved by CoolDadTx Tuesday, July 18, 2017 1:51 PM ASP.NET related
    Monday, July 17, 2017 9:07 PM

All replies