LoginSignup
4
2

More than 5 years have passed since last update.

Error: $resource:badcfg

Posted at

Response does not match configured parameter

Error in resource configuration for action get. Expected response to contain an object but got an array

Angularにてこのようなエラーが出たのでメモ。

かなりハマったが、
以下のAngularのドキュメントサイトに解決法が書いてありました。
https://docs.angularjs.org/error/$resource/badcfg

Description
This error occurs when the $resource service expects a response that can be deserialized as an array but receives an object, or vice versa. By default, all resource actions expect objects, except query which expects arrays.

デフォルトでは、すべてのリソースアクションは、アレイを想定されているクエリーを除いて、オブジェクトを期待しています。

サーバーサイド(ASP.NET)から返していたデータは以下

public IHttpActionResult Get()
{
    var result = new List<object>();
    return Ok(result);
}

Listを直接吐き出していたのが原因だった模様
以下に変更
csharp
public IHttpActionResult Get()
{
var list = new List<object>();
var result = new
{
message = "success",
data = list
};
return Ok(result);
}

これでデータを正常に受信できるようになりました。


return fooSvc.get(queryArgs).$promise.then(
    function (result) {
        return result.$promise;
    }, function (result) {
        return $q.reject(result);
    });

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2