[英]JSON/XML default response format change between Grails 2.2.3 and 2.3.11
The root symptom of our issue is that after switching our project from Grails 2.2.3 to 2.3.11, our jQuery Ajax calls started returning XML instead of JSON.我们问题的根本症状是在将我们的项目从 Grails 2.2.3 切换到 2.3.11 之后,我们的 jQuery Ajax 调用开始返回 XML 而不是 JSON。
The code snippets below illustrate how things are configured in our GSP, controllers, and service and show an example of a static data set that is returned as an object (a list of maps containing string pairs of code/name values).下面的代码片段说明了如何在我们的 GSP、控制器和服务中配置事物,并展示了作为对象返回的静态数据集的示例(包含代码/名称值字符串对的映射列表)。 The controller returns this list using the withFormat clause to the GSP.
控制器使用 withFormat 子句将此列表返回给 GSP。 In Grails 2.2.3, this was always JSON, but now in 2.3.11, it is XML.
在 Grails 2.2.3 中,这始终是 JSON,但现在在 2.3.11 中,它是 XML。
Through experimentation, I found that if I change the order of the JSON and XML lines in the withFormat clause in the controller to put JSON first, then everything works.通过实验,我发现如果我将控制器中withFormat 子句中JSON 和XML 行的顺序更改为将JSON 放在首位,则一切正常。 I do not like the idea of changing every action in every controller to make this work again.
我不喜欢改变每个控制器中的每个动作以使其再次工作的想法。
myTest.gsp myTest.gsp
var fetchData = function () {
$.ajax({
url: "${g.createLink(controller:'myController', action:'myAction')}",
async: true,
type: 'get',
dataType: "json",
success: function (data) {
if (data) {
// Not shown -- Do something useful with the data
}
},
error: function (request, status, error) {
show.alert("Error with the data. \nStatus: " + status + ", \nError: " + error );
}
});
};
MyController.groovy我的控制器.groovy
class MyController {
def myService
...
def myAction() {
def results = myService.myAction()
withFormat {
xml { render results as XML }
json { render results as JSON }
}
}
...
}
MyService.groovy MyService.groovy
class MyService {
def myMap = [ AK: 'Alaska', AL:'Alabama', ... , WY:'Wyoming' ]
def myAction() {
def results = []
myMap.each {
def item = [:]
item.code = it.key
item.name = it.value
result.add(item)
}
return results
}
}
Config.groovy配置文件
grails.mime.use.accept.header = true
UPDATE:更新:
I have a "fix", but I'm not very happy with it and would welcome alternate answers or explanations as to why this functionality broke between 2.2.3 and 2.3.11.我有一个“修复”,但我对它不是很满意,并欢迎关于此功能为何在 2.2.3 和 2.3.11 之间中断的替代答案或解释。
I changed the order of the JSON and XML types in the withFormat closure in my controller action to put JSON first and the issue is "resolved".我在我的控制器操作中更改了 withFormat 闭包中 JSON 和 XML 类型的顺序,将 JSON 放在首位,并且问题已“解决”。
I am not happy with this as it will require me to change all 68 of my actions in all of my controllers.我对此并不满意,因为它需要我更改所有控制器中的所有68个操作。 This is introducing a lot of potential risk as other functionality may change with me changing this volume of code all for something that worked fine in a previous version of Grails.
这引入了很多潜在的风险,因为其他功能可能会随着我更改这些代码量而发生变化,所有这些代码都可以在以前版本的 Grails 中正常工作。 Is there something at a global level that I can change to address this?
在全球范围内,我是否可以改变一些东西来解决这个问题?
MyController.groovy我的控制器.groovy
class MyController {
def myService
...
def myAction() {
def results = myService.myAction()
withFormat {
json { render results as JSON } // <-- MOVED THIS TO BE FIRST
xml { render results as XML }
}
}
...
}
Based on this article ( http://mrhaki.blogspot.com/2014/07/grails-goodness-enable-accept-header.html ) regarding accept headers, I added the following line to my Config.groovy and it corrected my issue.基于这篇关于接受标头的文章( http://mrhaki.blogspot.com/2014/07/grails-goodness-enable-accept-header.html ),我在我的 Config.groovy 中添加了以下行并纠正了我的问题.
Config.groovy配置文件
grails.mime.use.accept.header = true
grails.mime.disable.accept.header.userAgents = [] // <-- Added this line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.