I am toying around with AngularJS and ASP.Net's Web API working together. I have a TestController in the API that's as simple as it gets:
public class TestController : ApiController {
[HttpGet]
public String Ping() {
return "Pong";
}
}
In Chrome I can go to http://localhost/api/Test/Ping and fiddler shows a simple "Pong" result and the browser shows:
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Pong</string>
Back in Angular JS, I setup a factory to call the Ping function:
app.factory('API', ['$resource', function ($resource) {
return {
Ping: function () {
var result = $resource('api/Test/Ping', {}, { get: { method: 'GET' }, isArray: false });
return result.get();
}
};
}]);
And a super simple controller:
app.controller('MyCtrl', [ '$scope', 'API', function ($scope, API) {
$scope.CallTest = function () {
API.Ping().$promise.then(function (response) {
alert(response);
});
}
}]);
When I click the button that CallTest is bound to, it makes the call, the API returns Pong like it should but the return object is not exactly what I would expect. Response is an odd object:
{
0: """,
1: "P",
2: "o",
3: "n",
4: "g",
5: """,
$promise: {...},
$resolved: true
}
I don't receive any errors and everything syntactically is working fine. I was however hoping that response would be a string, especially since I set isArray to false in my API factory. I believe that angular has to return a "Resource" that has a $promise and $resolved on it so I now understand it may not work that way. Other than making a trivial wrapper in the WebAPI to return a string as a parameter on a Model, are there options available client side so that response could contain a normal string instead of this pseudo array? Like maybe response.data or something?
EDIT: When I request from the browser, the Accept header contains:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Which results in the XML result above. When Angular requests the same url, the Accept header contains:
application/json, text/plain, */*
Which results in the content of the response simply being "Pong"