Jquery Grails2.2.3和2.3.11之间的JSON/XML默认响应格式更改
我们问题的根本症状是,在将项目从Grails2.2.3切换到2.3.11之后,我们的jQueryAjax调用开始返回XML而不是JSON 下面的代码片段说明了如何在GSP、控制器和服务中进行配置,并显示了作为对象返回的静态数据集的示例(包含代码/名称值字符串对的映射列表)。控制器使用withFormat子句将此列表返回给GSP。在Grails2.2.3中,这始终是JSON,但在2.3.11中,它是XML 通过实验,我发现如果我在控制器的withFormat子句中更改JSON和XML行的顺序,将JSON放在第一位,那么一切都会正常工作。我不喜欢改变每个控制器中的每个动作以使其再次工作的想法Jquery Grails2.2.3和2.3.11之间的JSON/XML默认响应格式更改,jquery,json,ajax,grails,gsp,Jquery,Json,Ajax,Grails,Gsp,我们问题的根本症状是,在将项目从Grails2.2.3切换到2.3.11之后,我们的jQueryAjax调用开始返回XML而不是JSON 下面的代码片段说明了如何在GSP、控制器和服务中进行配置,并显示了作为对象返回的静态数据集的示例(包含代码/名称值字符串对的映射列表)。控制器使用withFormat子句将此列表返回给GSP。在Grails2.2.3中,这始终是JSON,但在2.3.11中,它是XML 通过实验,我发现如果我在控制器的withFormat子句中更改JSON和XML行的顺序,将J
- Grails2.3.x中的哪些更改打破了现有功能李>
- 我们如何调用这些函数以获得所需的JSON功能而不是XML,我们可以改变什么李>
- 这似乎应该能够在应用程序级别进行配置,而不是更改每个jQueryAjax调用或更改每个控制器操作
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
class MyController {
def myService
...
def myAction() {
def results = myService.myAction()
withFormat {
xml { render results as XML }
json { render results as JSON }
}
}
...
}
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
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 }
}
}
...
}
grails.mime.use.accept.header = true
grails.mime.disable.accept.header.userAgents = [] // <-- Added this line
更新:
我有一个“修复”,但我对它不是很满意,并欢迎其他答案或解释为什么该功能在2.2.3和2.3.11之间中断
我在控制器操作中更改了withFormat闭包中JSON和XML类型的顺序,将JSON放在第一位,问题就“解决了”
我对此不满意,因为这将要求我更改所有控制器中的所有动作。这带来了很多潜在的风险,因为其他功能可能会随着我更改这卷代码而发生变化,所有这些都是为了在以前版本的Grails中运行良好的东西。在全球层面上,我是否可以改变一些东西来解决这个问题
MyController.groovy
grails.mime.use.accept.header = true
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 }
}
}
...
}
grails.mime.use.accept.header = true
grails.mime.disable.accept.header.userAgents = [] // <-- Added this line
类MyController{
def myService
...
def myAction(){
def results=myService.myAction()
withFormat{
json{render results as json}/基于本文()中关于accept头的内容,我在Config.groovy中添加了以下行,它纠正了我的问题
Config.groovy
grails.mime.use.accept.header = true
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 }
}
}
...
}
grails.mime.use.accept.header = true
grails.mime.disable.accept.header.userAgents = [] // <-- Added this line
grails.mime.use.accept.header=true
grails.mime.disable.accept.header.userAgents=[]//您可以检查请求并查看您收到的是什么类型的响应,看起来您收到的是xml响应而不是json响应。请确认相同。是的。我们现在返回的是xml,而不是json,但我不确定2.2.3和2.3.11之间发生了什么变化,或者如何使其重新工作。您能给我们Accept标头o吗f Ajax请求?还可以尝试将Accept头设置为application/json。请检查此处