Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS$资源响应作为ExpressJS中的字符数组返回_Javascript_Angularjs_Express - Fatal编程技术网

Javascript AngularJS$资源响应作为ExpressJS中的字符数组返回

Javascript AngularJS$资源响应作为ExpressJS中的字符数组返回,javascript,angularjs,express,Javascript,Angularjs,Express,我有一个ExpressJSAPI,angularJS$资源对象可以与之对话。我已经向postman(一个测试RESTAPI的chrome工具)发送了一个post请求,响应中的原始数据是:“已提交” 标题: Connection →keep-alive Content-Length →9 Content-Type →text/html; charset=utf-8 Date →Sun, 02 Feb 2014 12:02:20 GMT X-Powered-By →Express 当我在angul

我有一个ExpressJSAPI,angularJS$资源对象可以与之对话。我已经向postman(一个测试RESTAPI的chrome工具)发送了一个post请求,响应中的原始数据是:“已提交”

标题:

Connection →keep-alive
Content-Length →9
Content-Type →text/html; charset=utf-8
Date →Sun, 02 Feb 2014 12:02:20 GMT
X-Powered-By →Express
当我在angular中注销我的响应时,我得到以下信息:

Resource
    0: "S"
    1: "u"
    2: "b"
    3: "m"
    4: "i"
    5: "t"
    6: "t"
    7: "e"
    8: "d"
    $promise: undefined
    $resolved: true
    __proto__: Resource
我的快递代码:

exports.create = function(req, res) {
    new Product(req.body).save(function(err) {
        if (err) {
            res.send('There was an error: ' + err); 
        }
        else {
            res.send('Submitted')
        }
    });
};
AngularJs工厂:

pantherServices.factory('Product', function($resource, Defaults) {
    var Product = $resource(Defaults.api_url + 'products', {productId: '@productId'} , {
            find: {
                method: 'GET',
                url: Defaults.api_url + 'products/:productId',
            },
            all: {
                method: 'GET',
                isArray: true
            }
        });

    return Product
});
我的控制器:

$scope.newProduct = {
    name: null,
    description: null,
    price: null,
    display_price: null,
    date_available: null
};

$scope.addNewProduct = function() {
    var newProduct = new Product($scope.newProduct);
    newProduct.$save(function(response, headers) {
        console.log(response)
    });
};
为什么它要分解字符并将响应解析为数组,这是我的头、angularjs还是express的问题

谢谢


编辑:res.json也有同样的问题。

我通过返回一个对象而不是字符串来解决这个问题


res.send({response:'Created new Product Object'})

在angular resource中,可以选择包装响应
transformResponse
,这应该可以解决问题

   $resource(appConfig.apiBaseUrl + '/newsletter/:id', {}, {
      update: {method: 'PUT'},
      weekly: {
        method: 'POST', params: {id: 'weekly'}, transformResponse: function (response) {
          // whatever you want to do
          return {html: response};
        }
      }
    });

我在我的ASP.NET WebApi应用程序中有相同的项目。通过返回字典对象修复。