I work together with AngularJS and ASP.Net Web API working together. I have a TestController in an API that is just as simple:
public class TestController : ApiController { [HttpGet] public String Ping() { return "Pong"; } }
In Chrome, I can go to http://localhost/api/Test/Ping , and the violinist 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 am setting up 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 on the button that CallTest is bound CallTest , it makes a call, the API returns Pong as it should, but the returned object is not quite what I would expect. The answer is an odd object:
{ 0: """, 1: "P", 2: "o", 3: "n", 4: "g", 5: """, $promise: {...}, $resolved: true }
I am not getting any errors and everything is syntactically working fine. However, I was hoping that response would be a string, especially since I set isArray to false in my factory API . I believe Angular should return a βResourceβ that has $promise and $resolved , so now I understand that this may not work. Besides creating a trivial wrapper in WebAPI to return a string as a parameter in Model, are there any options on the client side so that response can contain a regular 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 leads to the XML result above. When Angular requests the same url, the Accept header contains:
application/json, text/plain, */*
This leads to the fact that the content of the response is simply "Pong"