Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8778405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:31:50+00:00 2026-06-13T19:31:50+00:00

In WebAPI, say I return a string wrapped in an HTTP response: return Request.CreateResponse(HttpStatusCode.BadRequest,

  • 0

In WebAPI, say I return a string wrapped in an HTTP response:

return Request.CreateResponse(HttpStatusCode.BadRequest, "Line1 \r\n Line2");

When invoking this action from jQuery, the response text is treated before it is returned. So in the xhr, I get something like this:

function success(xhr) {
    alert(xhr.responseText); // alerts ""Line1 \\r\\n Line2""
}

In other words, the string gets wrapped in double quotes, and special characters get escaped so that they appear in the output (actual alert is “Line1 \r\n Line2”, so the newlines are not preserved, but rather encoded and shown in the response text).

I can get around this by removing the quotes and replacing the newlines on the client like so:

var responseText = xhr.responseText.substr(1, xhr.responseText - 2)
    .replace('\\r', '\r').replace('\\n', '\n');

But is there a way to tell WebAPI how to format string responses? For example, not to wrap them in double quotes and convert escaped characters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:31:51+00:00Added an answer on June 13, 2026 at 7:31 pm

    This happens because your controller is returning JSON in which string values are quoted.

    A simple solution is to parse the responseText as JSON and then you can use the value as intended:

    $.ajax("/api/values/10", {
        error: function (xhr) {
            var error = JSON.parse(xhr.responseText);
            $("textarea").val(error);
        }
    });
    

    This correctly interprets the line breaks/carriage returns.

    Alternatively you can specify the text/plain media type in your controller:

    return Request.CreateResponse(
        HttpStatusCode.BadRequest, 
        "Line1 \r\n Line2", "text/plain");
    

    Web API will then try and load an appropriate media type formatter for text/plain which unfortunately does not exist OOTB. You’ll find one in WebApiContrib.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let’s say I’ve got a simple Web API controller. I want to return a
My first post - firstly have to say that this site is great -
so i am trying to do a general return for all WebAPI calls to
Let say I have two methods in MVC 4 Web API controller: public IQueryable<A>
I had the following idea: Say we have a webapp written using django which
How can I host, say a Snap webapp, on amazon? In other words, what
Does WebAPI support SOAP? I'm trying to write a SOAP Server in MVC4 and
Upgrading from ASP.NET WebAPI Beta to RC has provided some amount of excitement and
I am using mvc webapi to create a REST API and struggling to find
I have a desktop webapi server that I want to expose to clients through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.