简体   繁体   English

Grails - 使用JSON从控制器通过Ajax渲染Map

[英]Grails - render Map from controller via Ajax using JSON

I know this is a recurrent/classical topic but I did not found anything that helped me so far. 我知道这是一个经常性/经典的话题,但到目前为止我找不到任何帮助我的东西。 I am trying to render a Map from my controller. 我试图从我的控制器渲染一个地图。 This results from an Ajax request and is supposed to be "eaten" by a Javascript function 'onSuccess'. 这是由Ajax请求产生的,并且应该由Javascript函数“onSuccess”“吃掉”。

Here is my Javascript and .gsp view : 这是我的Javascript.gsp视图

<g:javascript>
function updateVideoLoad(e) {
var map = eval("("+e.responseText+")") // evaluate the JSON
$('#resultsChecker').html(map.urlAccepted + ' - ' + map.provider + ' - ' + map.videoId + ' - ' + map.videoTag)
}
</g:javascript>
<g:formRemote name="myForm" update="" url="[controller: 'project', action:'addVideo']" onSuccess="updateVideoLoad(e)">        
    ...      
</g:formRemote>

Here is my controller : 这是我的控制器

    import grails.converters.JSON

class ProjectController {

    def addVideo() {
       ...
    def videoMap = [urlAccepted: videoList[0], provider: videoList[1], videoId: videoList[2], videoTag: videoList[3]]
    render videoMap as JSON
    }

It looks to me exactly as the example provided in the Grails documentation . 它完全符合我在Grails文档中提供的示例。 However it does not work. 但它不起作用。 On the browser console, I get: 在浏览器控制台上,我得到:

Uncaught ReferenceError: e is not defined

from my g:remoteForm . 来自我的g:remoteForm

Any suggestion is most welcome. 任何建议都是最受欢迎的。 Thank you for your help. 谢谢您的帮助。

I'm guessing here, but it looks like a mistake in the documentation. 我在这里猜测,但它在文档中看起来像是一个错误。 I would expect that this part: 我希望这部分:

 onSuccess="updateVideoLoad(e)"
                           ^^^

Should really be: 真的应该是:

 onSuccess="updateVideoLoad(data,textStatus)"
                           ^^^^^^^^^^^^^^^^^

Looking at the generated code (under Grails 2.0), these are the variables used in the returned: 查看生成的代码(在Grails 2.0下),这些是返回的变量:

<form onsubmit="jQuery.ajax({type:'POST',data:jQuery(this).serialize(), url:'/project/addVideo',success:function(data,textStatus){updateVideoLoad(e);},error:function(XMLHttpRequest,textStatus,errorThrown){}});return false"
    method="post" action="/project/addVideo" id="myForm">

Look at the success property in the generated code. 查看生成的代码中的success属性。

This seems to be working: 这似乎有效:

<g:formRemote name="myForm" update="" url="[controller: 'project', action:'addVideo']" onComplete="updateVideoLoad(XMLHttpRequest)">

Is there a place where to get documentation on those javascript events ( onComplete , onSuccess , ...) ? 有没有地方可以获得有关这些javascript事件的文档( onCompleteonSuccess ,...)?

This appears to be a documentation bug. 这似乎是一个文档错误。 If using JQuery (the default as of Grails 2.0) The variable should be 'data' not 'e' and it will contain your JSON object that you returned from your controller, and there is no need to eval or parse it like in the docs. 如果使用JQuery(Grails 2.0的默认值)变量应该是'data'而不是'e'并且它将包含您从控制器返回的JSON对象,并且不需要像在文档中那样评估或解析它。

I had the same problem, but I'm using Dojo. 我有同样的问题,但我正在使用Dojo。

If you use dojo, you shoud use the variable name 'response': 如果你使用dojo,你应该使用变量名'response':

onComplete="updateVideoLoad(response)"

It works fine for Dojo. 它适用于Dojo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM