我的 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":{"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"}]
如果我将控制器中的响应从 render json 更改为 grails.converters.JSON; to render(view: "list", contentType: "application/json") 我被带到 list.gsp 页面,但是 response.responseText 是完整的 html 文本,而不是 json 字符串。任何线索为什么?
对此的任何帮助,以及理解 grails 控制器、json、ext ajax 将不胜感激。