使用条件运算符在Grails中渲染“作为JSON”无法正确渲染

使用条件运算符在Grails中渲染“作为JSON”无法正确渲染,第1张

使用条件运算符在Grails中渲染“作为JSON”无法正确渲染

这是的正常行为

render
。当您提供
render
括号参数

render model as JSON

进行隐式调整以设置

content-type
to
text/json
。但是,在后一种情况下,你已经在不知不觉中进行的
render
使用像括号[标记的第一支撑后
render
品牌呈现正常使用的
render()
]

render ((products) ? [products:products] : [products:"No products found"]) asJSON

在上述情况下,你必须在指定的参数传递给

render
提了
contentType
text
或者
model
status
等于是为了使在线控制逻辑JSON在浏览器/查看您所要做的象下面这样:

render(contentType: "application/json", text: [products: (products ?: "No products found")] as JSON)

您还可以使用

content-type
作为
text/json
。我更喜欢
application/json

更新
最简单的方法:

render([products: (products ?: "No products found")] as JSON)



欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/5630036.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存