In WebAPI, say I return a string wrapped in an HTTP response:
return Request.CreateResponse(HttpStatusCode.BadRequest, \"Line1 \\r\\n Line2\");
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.