gpt4 book ai didi

json - Ext Ajax请求未从Grails Controller 接收JSON作为response.responseText

转载 作者:行者123 更新时间:2023-12-02 14:54:47 25 4
gpt4 key购买 nike

我的grails应用程序中有一个 Controller ,该 Controller 使用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,“时间”:1334648869000,"new":false},“_ class”:“com.mongodb.BasicDBObject”,“form1name”:“IHLForm”,“form2name”:“CCDForm”,“form3name”:“AHDForm“,” form4name“:” ServicecnteraForm“,” form5name“:” ISForm“}]
  • 如果我将 Controller 中的响应从
    将json渲染为grails.converters.JSON;

    渲染( View :“列表”,contentType:“应用程序/ json”)
    我被带到list.gsp页面,但是response.responseText是完整的html文本,而不是json字符串。有什么线索吗?

  • 任何帮助,并了解grails Controller ,json,ext ajax将不胜感激。

    最佳答案

    我不习惯grails。但是,在阅读您的代码后,出现了一些错误,因此,我尝试解释您的2个问题:

    .1: use "render json as grails.converters.JSON":



    运行Controller时,未指定要渲染/重定向的 View ;因此JSON字符串将返回并显示在浏览器上(即尚未带到list.gsp页面)

    .2: change the response in the controller from render json as grails.converters.JSON; to render a new view: "list", contentType: "application/json")



    您可以直接在 View 中访问json结果,因此您无需在此处使用Ajax。只需回显它或做任何您想做的事即可。

    希望这可以帮助。

    关于json - Ext Ajax请求未从Grails Controller 接收JSON作为response.responseText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10410099/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com