【发布时间】:2014-08-03 21:16:25
【问题描述】:
我正在使用以下 jQuery 代码将我的参数发送到我的 Grails 应用程序:
$.ajax({
url:'<g:createLink controller="myController" action="save"/>',
data: { name: 'erik' },
type: 'POST',
contentType: "application/json",
dataType: 'json',
success: function(data) {
// Do something with the request
}
});
在我的 Grails 应用程序中,我有以下操作:
@Transactional
def save(Speaker speakerInstance) {
if (speakerInstance == null) {
notFound()
return
}
if (speakerInstance.hasErrors()) {
respond speakerInstance.errors, view:'create'
return
}
speakerInstance.save flush:true
request.withFormat {
form {
flash.message = message(code: 'default.created.message', args: [message(code: 'speakerInstance.label', default: 'Speaker'), speakerInstance.id])
redirect speakerInstance
}
'*' { respond speakerInstance, [status: CREATED] }
}
}
但是,演讲者的名字没有填写。如果我删除 contentType,代码可以工作,但最终会出现在 'form' 闭包中,我想结束在 '*' 部分。
我尝试使用params.name获取名称,使用request.JSON.name,但没有获取参数,但根据Chrome,参数已发送,当我从请求中删除contentType时,它可以工作。我在这里错过了什么?
【问题讨论】: