接收角作为字符串数组? [英] Angular receives String as array?

查看:231
本文介绍了接收角作为字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我玩弄与周围AngularJS和ASP.Net的Web API一起工作。我有,因为它得到这简单的API中的TestController:

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";
    }
}

在Chrome浏览器,我可以去的http://本地主机/ API /测试/平和提琴手显示了一个简单的结果和浏览器显示:

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>

早在角JS,我建立一个工厂调用Ping功能:

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);
        });
    }
}]);

当我点击 CallTest 势必会,它使呼叫按钮,API返回傍像它应该,但返回的对象不正是我所期望的。响应是一个奇怪的对象:

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
}

我没有收到任何错误和语法上的一切工作正常。但我希望,响应将是一个字符串,尤其是因为我设置 IsArray的在我的<$ C $假C> API 工厂。我相信,角度必须返回具有 $承诺 $解决就可以了,所以我现在是一个资源了解它可能不是这样的。不是使一个简单的包装中的WebAPI返回一个字符串作为模型的参数外,有可供选择的客户端,这样响应可能包含一个普通的字符串,而不是这个伪数组?也许就像 response.data 还是什么?

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?

编辑:当我从浏览器请求中,接受头包含:

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

,这导致在上面的XML结果。当角请求相同的URL,在接受头包含:

application/json, text/plain, */*

这会导致反应仅仅是内容

推荐答案

由于在该文档中描述的, $资源一家工厂创建资源对象,让你REST风格的服务器端数据源

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

在你的情况,你是不是想用适当的REST风格的数据源进行交互,从而行为似乎有些奇怪。

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

除非你有一个正确的REST风格的数据源,而你只是想打一个API端点和检索一个简单的反应(如字符串),你应该使用 $ HTTP 服务

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) {...})...

例如:

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);
        });
    };
}]);

这篇关于接收角作为字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆