1

我的 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>
  1. 在这一步之后,当我加载浏览器并调用 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"}]

  2. 如果我将控制器中的响应从 render json 更改为 grails.converters.JSON; to render(view: "list", contentType: "application/json") 我被带到 list.gsp 页面,但是 response.responseText 是完整的 html 文本,而不是 json 字符串。任何线索为什么?

对此的任何帮助,以及理解 grails 控制器、json、ext ajax 将不胜感激。

4

1 回答 1

0

我不习惯grails。但是,在阅读您的代码后,有些地方不正确,所以我尝试解释您的 2 个问题:

.1:使用“将 json 渲染为 grails.converters.JSON”:

当你运行控制器时,你没有指定要渲染/重定向的视图;因此 JSON 字符串被返回并显示在浏览器上(即尚未带到 list.gsp 页面)

.2:将控制器中的响应从render json更改为grails.converters.JSON;渲染一个新视图:“list”,contentType:“application/json”)

可以直接在视图中访问json结果,所以这里不需要使用Ajax。只是回应它或做任何你想做的事情。

希望这可以帮助。

于 2012-05-05T04:50:24.243 回答