2014-02-17 54 views
0

對於用角度測試我有一個Webapi方法,它返回一個包含List的對象的實例。 (json的輸出可以在下面看到)Angluar.js和WebApi返回int數組

現在在我的角度控制器中我想檢索這個對象。

有沒有人有暗示我做錯了什麼?

RestResource爲工廠

myngApp.factory("valueResource", function ($resource) { 

    var result = 
     { 
      database: $resource("/api/values") 
     } 

    return result; 
}); 

控制器

myngApp.controller("dataController", function ($scope, settings, valueResource) { 
    $scope.message = "Hello World!"; 
    $scope.userName = settings.user; 


    $scope.restValues = valueResource.database.query({}); 
}); 

JSON響應(使用Fiddler採取)是這樣的:

HTTP/1.1 200 OK 
Cache-Control: no-cache 
Pragma: no-cache 
Content-Type: application/json; charset=utf-8 
Expires: -1 
Server: Microsoft-IIS/8.0 
X-AspNet-Version: 4.0.30319 
X-SourceFiles: =?UTF-8?B?RDpca2lsblxleHBlcmltZW50YWxcc3JjXG5nMlxXZWJBcHBsaWNhdGlvbjFcV2ViQXBpXHZhbHVlcw==?= 
X-Powered-By: ASP.NET 
Date: Mon, 17 Feb 2014 14:47:35 GMT 
Content-Length: 19 

{"Items":[1,2,3,4]} 

控制檯輸出爲:

XHR finished loading: "http://localhost:55671/api/values". angular.js:8013 
Error: [$resource:badcfg] http://errors.angularjs.org/1.2.12/$resource/badcfg?p0=array&p1=object 
    at Error (native) 
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:6:450 
    at p.then.m.$resolved (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular-resource.min.js:8:517) 
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:93:5) 
    at A (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:93:5) 
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:94:173 
    at h.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:102:456) 
    at h.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:100:218) 
    at h.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:103:264) 
    at f (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js:67:120) angular.js:9435 

回答

1

documentation for $resource表明,創建模型應該以不同的方式來完成。如果你只是想對這個URL做一個XHR請求,我會使用$ http服務來解決在視圖中返回的promise。在誤差