我的grails应用程序中有一个控制器,它使用HTTPBuilder进行调用,如下面的代码所示:
import grails.converters.deep.JSON
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.Method.GET
class NotificationController {
def index() {
redirect(action: "list", params: params)
}
def list()
{
println "in List Method of Notification Controller"
def http1 = new HTTPBuilder("http://localhost:8082")
// perform a GET request, expecting JSON response data
http1.request(groovyx.net.http.Method.GET, groovyx.net.http.ContentType.JSON)
{
uri.path = '/Dummy/CASearchService/dummysearch/meta'
// response handler for a success response code:
response.success = { resp, json ->
println resp.statusLine
// parse the JSON response object:
println("json -- "+json)
println((json as grails.converters.JSON).class)
render json as grails.converters.JSON;
}
// handler for any failure status code:
response.failure = { resp ->
println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
}
}
}
}
现在,这将返回一个json字符串作为响应,因为println(json)返回一个json字符串:
[[_id:[machine:680383044, inc:149754531, time:1334648778000, new:false], _class:com.mongodb.BasicDBObject, form1name:EmpForm, form2name:DeptForm, form3name:HRForm, form4name:AdminForm, form5name:FSIForm], [_id:[machine:680339682, inc:-2056232867, time:1334648869000, new:false], _class:com.mongodb.BasicDBObject, form1name:IHLForm, form2name:CCDForm, form3name:AHDForm, form4name:ServicecnteraForm, form5name:ISForm]]
在通知下的list.gsp文件中,我有以下Ext.Ajax调用:
<html>
<head>
<meta name="layout" content="ext"/>
<title>List of Notifications</title>
<script>
Ext.Ajax.request({
url: '${createLink( action: 'list' )}',
success: function (response){
//Response json object
alert("SUCCESS")
var jsonData = (response.responseText);
alert(jsonData)
},
failure: function (response){
alert("Failure")
}
});
</script>
</head>
<body>
<div class="body">
In List gsp of Notification Controller
</div>
</body>
</html>
在此步骤之后,当我加载浏览器并调用NotificationController时,我将获得直接在浏览器中显示的json字符串。为什么会发生这种情况,为什么调用不会转到Ajax.request.success?
[{ “_ ID”:{ “机器”:680383044 “INC”:149754531, “时间”:1334648778000, “新”:假}, “_类”: “com.mongodb.BasicDBObject”, “form1name” : “EmpForm”, “form2name”: “DeptForm”, “form3name”: “HRForm”, “form4name”: “AdminForm”, “form5name”: “FSIForm”},{ “_ ID”:{ “机器”:680339682, “公司”: - 2056232867, “时间”:1334648869000, “新”:假}, “_类”: “com.mongodb.BasicDBObject”, “form1name”: “IHLForm”, “form2name”: “CCDForm”,“form3name “:” AHDForm”, “form4name”: “ServicecnteraForm”, “form5name”: “ISForm”}]
如果我更改控制器中的响应 渲染json为grails.converters.JSON; 至 render(view:“list”,contentType:“application / json”) 我被带到list.gsp页面,但response.responseText是完整的html文本,而不是json字符串。有什么线索?
对此有任何帮助,理解grails控制器,json,ext ajax将不胜感激。
答案 0 :(得分:0)
我还不熟悉grails。但是,在阅读完代码后,有些内容不正确,所以我尝试解释您的两个问题:
.1:使用“render json as grails.converters.JSON”:
运行Controller时,不指定要渲染/重定向的视图;所以返回JSON字符串并在浏览器上显示(即尚未转到list.gsp页面)
.2:将渲染器中的响应从渲染json更改为grails.converters.JSON;渲染新视图:“list”,contentType:“application / json”)
您可以直接在视图中访问json结果,因此您不需要在此处使用Ajax。只需回应它或做任何你想做的事。
希望这有帮助。