角$资源不能正确分析整数响应 [英] Angular $resource does not parse correctly an integer response

查看:214
本文介绍了角$资源不能正确分析整数响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的角度$资源提出要求到我的控制器,在Spring应用程序来实现。
当控制器只返回一个整数值,$资源解析它坏了。
使用Firebug检查它,我得到这样的:

I am using the Angular $resource to make requests to my controllers, implemented on a Spring application. When the controller returns just an Integer value, $resource parse it bad. Inspecting it with Firebug I get something like:

Resource { 0="1", 1="9", 2="1", more...}

,其中191只是intger值服务器返回。
与其他复杂对象不麻烦(由服务器解析JSON)。
建议?
谢谢
FB

where 191 is just the intger value the server returns. No trouble with others complex object (parsed in JSON by the server). Suggestions? Thanks FB

推荐答案

我知道这是有点老了,但我只是碰到了类似的问题,有人指着我正确的方向。

I know this is a little old, but I just ran into a similar problem and someone pointed me in the right direction.

如果您的服务器发回一个JSON EN codeD resonse,但你只传递一个值(191你的情况),$资源似乎间preT作为一个字符串,它是然后分成单个的字符数组(我不是为什么技术细节100%,但是这是我的经验)。所以,你需要做的是回送包裹的JSON对象内你的价值。

If your server is sending back a JSON encoded resonse, but you only pass it a single value (191 in your case), $resource seems to interpret it as a string, which it is then splitting into an array of individual characters (I'm not 100% on the technical details of why, but that was my experience). So what you need to do is send back your value wrapped inside of a JSON object.

所以,与其做这样的事情:

So, instead of doing something like:

function callback(req, res) {
    var number = 191;  // this is just for example purposes
    res.json(number);
}

您需要做的是这样的:

function callback(req, res) {
    var number = 191;  // again, just for an example
    res.json({ value: number });
}

然后当你$资源来电回来,刚进入值的响应属性,你应该是好去。

P.S。这里的例子是基于节点/ EX preSS后端,而不是春天,但我认为这应该仍然适用。

这篇关于角$资源不能正确分析整数响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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