Grails:request.JSON来自哪里,如何使用jQuery的.ajax()或.post()把东西放在那里? [英] Grails: where does request.JSON come from, and how do I put things there with jQuery's .ajax() or .post()?

查看:323
本文介绍了Grails:request.JSON来自哪里,如何使用jQuery的.ajax()或.post()把东西放在那里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,需要一些json在?请求正文,并做了很棒的事情:

 code> def myController(){
def myAction(){
println这里是request.JSON:$ {request.JSON as JSON}
println $ params
//使用request.JSON只做
return
}
}

所以我可以这样使用cURL:

  curl -i -H content-type:application / json-d{\someVariable\:\Absolutely\}

我的grails控制器打印:

 这里是request.JSON:{someVariable: Absolutely} 
这里是params:[controller:'myController',action:'myAction']


$ b b

到目前为止这么好,但是当我尝试用jQuery做这个时,它进入params !!!



阅读了这两个问题:
使用jQuery将POST主体设置为JSON对象



jQuery在请求主体中发布有效的json



我最好的猜测是如何编写.js:

  var sendMe = {someVariable:Absolutely} 
$ .ajax({
url:'/ myController / myAction',
类型:'POST',
processData:false,
data:JSON.stringify(sendMe),
dataType:'json',
success:function(data){

},
错误:function(request,status,error){

}
});

但是当我这样做时,我的控制器打印:

 这里是request.JSON:{} 
这里是params:[{someVariable:绝对} :, controller:'myController',action:'myAction ']

我必须在jQuery上做错事。



UPDATE:看起来像这个白痴实际上有同样的问题:如何获得JSON在grails 2.0 ,但不是面对jQuery问题,他只是使用cURL和reqeust.JSON的东西。一个懒惰的家伙。

解决方案

我几天前也有同样的麻烦。



对我来说 - 解决方案是使用 jQuery.ajaxSetup 来设置ajax内容类型的默认值。

  $(function(){
$ .ajaxSetup({
contentType:application / json; charset = utf-8
});
}

使用 $。 ajax $。post 将JSON传输到控制器,并使用它:

  def yourJSON = request.JSON 

t知道为什么 $。ajax $。post 中的'contentType'选项在我的测试期间被忽略。 p>

I have a controller that takes some json in the ?request body? and does awesome things with it:

def myController(){
  def myAction(){
    println "Here is request.JSON: ${request.JSON as JSON}"
    println "Here is params: $params"
    //do awesome stuff with request.JSON only
    return
  }
}

So I can hit this with cURL like so:

curl -i -H "content-type: application/json" -d "{\"someVariable\":\"Absolutely\"}"

And my grails controller prints:

Here is request.JSON: {"someVariable":"Absolutely"}
Here is params: [controller:'myController', action:'myAction']

So far so good, but when I try to do this with jQuery it goes in params!!!

Having read these two questions: Setting the POST-body to a JSON object with jQuery

jQuery posting valid json in request body

My best guess for how to write the .js is:

var sendMe = {"someVariable":"Absolutely"}
$.ajax({
  url: '/myController/myAction',
  type: 'POST',
  processData: false,
  data: JSON.stringify(sendMe),
  dataType: 'json',
  success: function(data) {

  },
  error: function(request, status, error) {

  }                     
});

But when I do this my controller prints:

Here is request.JSON: {}
Here is params: [{"someVariable":"Absolutely"}:, controller:'myController', action:'myAction']

I must be doing something wrong with jQuery.

UPDATE: looks like this idiot was actually having the same problem: How to get at JSON in grails 2.0 but instead of confronting the jQuery issue he just used cURL and the reqeust.JSON thing. What a lazy guy.

解决方案

I have the same trouble like you few days ago.

For me - the solution was to use jQuery.ajaxSetup to set the default value for ajax content type.

$(function() {
    $.ajaxSetup({
        contentType: "application/json; charset=utf-8"
    });
}

With this you could use $.ajax or $.post to transfer your JSON to the controller and use it like that:

def yourJSON = request.JSON

I dont't know why the 'contentType' option within $.ajax and $.post was ignored during my tests.

这篇关于Grails:request.JSON来自哪里,如何使用jQuery的.ajax()或.post()把东西放在那里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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