1

以下のコードに示すように、HTTPBuilderを使用してRESTful呼び出しを行うコントローラーがgrailsアプリケーションにあります。

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}"
            }
        }

    }
}

println(json)が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. コントローラの応答をレンダリングjsonからgrails.converters.JSONとして変更した場合。to render(view: "list"、contentType: "application / json")list.gspページに移動しましたが、response.responseTextはjson文字列ではなく、完全なhtmlテキストとして表示されます。なぜ手がかりはありますか?

これに関するヘルプ、およびgrailsコントローラー、json、extajaxを理解していただければ幸いです。

4

1 に答える 1

0

私はグレイルに慣れていません。ただし、コードを読んだ後、何かが間違っているので、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 に答える