如何从Grails Controller中的params获取地图? [英] Howto get a map from params in Grails Controller?

查看:342
本文介绍了如何从Grails Controller中的params获取地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在控制器中检索从客户端(通过ajax)以JSON格式发送的复杂对象,但是我不知道如何从params中获取转换了某些属性的映射。 / p>

例如,假设这是复杂的JSON对象(元对象中的项目数是可变的,可以是一个,两个,三个...和变量名):

  {
语言:java,
元:{
类别:category1
}
}

当此对象通过jQuery,在控制器我得到这在params对象中:

  [language:java,meta [category]:category1,action:注册,控制器:myController] 

这是我如何通过jQuery发送对象。我有几个电话的共同功能:

  if(!params)params = {}; 

var url = this.urls.base +/+ controller +/+ action +?callback =?;
if(params.callback)
url = this.urls.base +/+ controller +/+ action +?callback =+ params.callback;
url = url +& _+ new Date();
delete params.callback;
$ .ajax({
url:url,
data:params,
crossDomain:true,
dataType:'jsonp',
cache:false ,
ajaxOptions:{cache:false},
jsonp:params.callback?false:true
});

和在发送测试我之前写过的JSON对象的ajax调用的参数>

如果我尝试在控制器中做params.meta,我得到一个空对象。
我应该如何从params对象中检索地图?

解决方案

在客户端,您必须使用POST方法发送数据,并配置jQuery将其作为JSON发送。喜欢:

  data = {
语言:java,
元:{
类别:category1
}
}
$ .ajax({
type:'POST',
data:JSON.stringify(data),
contentType:'application / json',
})

request.JSON 请参阅文档: http://grails.org/doc/2.2.0/ref/Servlet%20API/request.html



但是,如果你需要使跨域请求,POST方法不起作用。在这种情况下,您可以将复杂对象作为参数传递,并从字符串中解析服务器。喜欢:

  $。ajax({
data:{myjson:JSON.stringify(data)}
})

和:

  def myjson = JSON.parse(params.myjson)


I'm trying to retrieve in the controller a complex object sent from the client (via ajax) in JSON format, but I don't know how to get from params the map in which are converted some of the properties.

For example, imagine this is the "complex" JSON object (the number of items in the meta object is variable, could be one, two, three... and with variable names):

{ 
  language: "java",
  meta: {
      category: "category1"
  }
}

When this object is sent via jQuery, in the controller I get this in params object:

[language:java, meta[category]:category1, action: register, controller: myController]

And this is how I send the object via jQuery. I have a common function for several calls:

if (!params) params = {};

var url = this.urls.base+"/"+controller+"/"+action+"?callback=?";
if (params.callback)
    url = this.urls.base+"/"+controller+"/"+action+"?callback="+params.callback;
url = url + "&_"+new Date();
delete params.callback;
$.ajax({
    url: url,
    data: params,
    crossDomain:true,
    dataType:'jsonp',
    cache:false,
    ajaxOptions: {cache: false},
    jsonp: params.callback?false:true
});

and in params for the ajax call I send for testing the JSON object that i wrote before

If I try to do params.meta in the controller, I get a null object. How I'm I supposed to retrieve the map from the params object?

解决方案

On client side you have to send data using POST method, and configure jQuery to send it as JSON. Like:

data = { 
  language: "java",
  meta: {
      category: "category1"
  }
}
$.ajax({
  type: 'POST',
  data: JSON.stringify(data),
  contentType: 'application/json',
})

And get on server side as request.JSON see docs: http://grails.org/doc/2.2.0/ref/Servlet%20API/request.html

But, if you need to make Cross Domain request, POST method just doesn't work. At this case you can pass your complex object as a parameter, and parse on server from the string. Like:

$.ajax({
  data: {myjson: JSON.stringify(data)}
})

and:

def myjson = JSON.parse(params.myjson)

这篇关于如何从Grails Controller中的params获取地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆