angularjs - Angular receives String as array? -
i toying around angularjs , asp.net's web api working together. have testcontroller in api that's simple gets:
public class testcontroller : apicontroller { [httpget] public string ping() { return "pong"; } }
in chrome can go http://localhost/api/test/ping
, fiddler shows simple "pong"
result , browser shows:
<string xmlns="http://schemas.microsoft.com/2003/10/serialization/">pong</string>
back in angular js, setup factory call 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 super simple controller:
app.controller('myctrl', [ '$scope', 'api', function ($scope, api) { $scope.calltest = function () { api.ping().$promise.then(function (response) { alert(response); }); } }]);
when click button calltest
bound to, makes call, api returns pong should return object not expect. response odd object:
{ 0: """, 1: "p", 2: "o", 3: "n", 4: "g", 5: """, $promise: {...}, $resolved: true }
i don't receive errors , syntactically working fine. hoping response
string, since set isarray
false in api
factory. believe angular has return "resource" has $promise
, $resolved
on understand may not work way. other making trivial wrapper in webapi return string parameter on model, there options available client side response
contain normal string instead of pseudo array? maybe response.data
or something?
edit: when request browser, accept
header contains:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
which results in xml result above. when angular requests same url, accept
header contains:
application/json, text/plain, */*
which results in content of response being "pong"
as described in docs, $resource
a factory creates resource object lets interact restful server-side data sources.
in case, not trying interact proper restful data source, behaviour seems strange.
until have proper restful data source , while want hit api endpoint , retrieve simple response (such string), should using $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); }); }; }]);
Comments
Post a Comment