Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular receives String as array?

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"

like image 947
Corey Ogburn Avatar asked Sep 10 '14 17:09

Corey Ogburn


1 Answers

As described in the docs, $resource is a factory which creates a resource object that lets you interact with RESTful server-side data sources.

In your case, you are not trying to interact with a proper RESTful data source, thus the behaviour seems strange.

Until you have a proper RESTful data source and while you just want to hit an API endpoint and retrieve a simple response (such as a string), you should be using the $http service:

$http.get('api/Test/Ping').success(function (data) {...})...

E.g.:

app.factory('API', ['$http', function ($http) {
    return {
        Ping: function () {
            return $http.get('api/Test/Ping');
        }
    };
}]);

app.controller('MyCtrl', ['$scope', 'API', function ($scope, API) {
    $scope.CallTest = function () {
        API.Ping().success(function (data) {
            alert(data);
        });
    };
}]);
like image 182
gkalpak Avatar answered Nov 15 '22 09:11

gkalpak