0

私のコントローラーは次のようになります。

  def save() {
    js {
       def color = new Color(params)
       color.save()
       def result
       if (!color.hasErrors()) 
       {
          result = [colorname: color.name, colorshde: color.shade]
       }
       else
       {
         result = "..."
       }
       render result as JSON
    }

 }

私が望む JSON は次のようになります。

成功した JSON

{
   "meta": {
      "status": 200,
      "msg": "OK"
   },
   "response": {
      "color": {
         "colorname": "Red",
         "shade": "light
      }
   }
}

失敗した応答:

{
   "meta": {
      "status": 400,
      "msg": "Something went worn"
   },
   "response": {
      "color": {
      }
   }
}

質問

json を返すときに両方のシナリオを考慮してコントローラー アクションを変更するにはどうすればよいですか?

4

1 に答える 1

0

正常な応答の場合:

{
"meta": {
  "status": 200,
  "msg": "OK"
},
"response": {
  "color": {
     "colorname": "Red",
     "shade": "light
  }
}
}

使用する:

result = [meta: [status: '200', msg: 'OK'], response:[color:[colorname: color.name, colorshde: color.shade] ] ]

失敗した応答の場合:

{
  "meta": {
  "status": 400,
  "msg": "Something went worn"
},
"response": {
  "color": {
  }
}
}

使用する

 result = [meta: [status: '400', msg: 'wrong'], response:[color:[] ] ]
于 2013-05-22T07:56:43.917 に答える