$ resource.query返回拆分字符串(字符数组)而不是字符串 [英] $resource.query return split strings (array of char) instead of a string

查看:78
本文介绍了$ resource.query返回拆分字符串(字符数组)而不是字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用像下面这样的有角度的$ resource.

I am using a angular $resource like the one below.

angular.module('app')
.factory('data', function ($resource) {

    var Con = $resource('/api/data', {}, {
        update : {method : 'PUT'}
    });

    return {     

        getData : function (user_id, callback) {

             return Con.query({user_id : user_id}, function (data) {
                 cb(data); // (breakpoint) HERE data is not good
             }, function (err) {
                 cb(err);
             }).$promise;
         }

   }; 
});

这是在数据上设置断点时得到的:

This is what I get when a put a breakpoint on data :

[
    ['w','e','l','c','o','m','e'],
    ['h','e','l','l','o']
] 

但是,服务器发送:

['welcome','hello']

有人知道为什么字符串会被分割吗?

anyone know why the strings get split?

谢谢

推荐答案

您已经遇到了angular的$ resource的一个有趣的错误,该错误无法处理原始的字符串数组;作为解决方法,您可以执行以下三种操作之一:

You've run into a fun bug with angular's $resource where it cannot handle a raw array of strings; as a workaround, you can do one of three things:

  • 改为使用$ http服务
  • 通过服务器发送对象包装的响应,例如:{ "stuff" : [ "your", "strings" ] }
  • 将响应数据强制转换为上述客户端格式; $ resource例如:methodName: {method:'GET', url: "/some/location/returning/array", transformResponse: function (data) {return {list: angular.fromJson(data)} }},然后以data.list
  • 的身份访问
  • use the $http service instead
  • send an object-wrapped response via the server eg: { "stuff" : [ "your", "strings" ] }
  • force the response data into the above format client-side; $resource eg: methodName: {method:'GET', url: "/some/location/returning/array", transformResponse: function (data) {return {list: angular.fromJson(data)} }} and then access it as data.list

https://stackoverflow.com/a/22491240/626810 中查看我的答案

这篇关于$ resource.query返回拆分字符串(字符数组)而不是字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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