Reputation: 2862
An ASP.NET Web Api function returns a simple string in JSON.
When I call this function from angularjs, I get a quoted string, not a simple string:
return $http.post('/api/orders', data).then(function (results) {
return result.data;
result.data is "my string"
, with quotes. It is due because the returned message is a string primitive, not a object. How is the appropriate way to deal with it? Removing quotes using a js function? Forcing to server to return a object instead of primitive? some special configuration? ...?
UPDATE:
The server uses a Web Api controller that returns a string:
public IHttpActionResult SaveOrder() {return Ok("this is a test");}
it has the same result that:
public string SaveOrder() {return "this is a test";}
The problem is that the returned JSON value is not an object, it is directly the string primitive.
Upvotes: 2
Views: 191
Reputation: 17064
I had the same problem. It happens because you are returning string
from your controller, I assume it looks something like this:
public string ControllerMethod(..) {...}
You should be returning HttpResponseMessage
instead of string
, and the object will be like this:
public HttpResponseMessage ControllerMethod()
{
var myString = "my string";
return new HttpResponseMessage()
{
Content = new StringContent(myString, Encoding.UTF8, "text/html")
};
}
Upvotes: 2