![](/img/trans.png)
[英]How to pass a table data from and HTML front end to the server side in Django through JSON?
[英]How to read json data from AngularJs (front end) to Grails (server side) through $http
我必须通过$ http服务方法将JSON数据从Angular发送到Grails。 JSON数据已发送到服务器。 但是在服务器端,它不会显示参数值。 我是Grails的新手。
我的代码:
控制器:
$(function(){
gateApp.factory('saveCreateToServer', function($http){
return {
saveDataToServer:function(taskCreateFormData){
console.log(taskCreateFormData);
return $http({
method : 'POST',
url : 'save',
data : taskCreateFormData,
})
}
}
});
gateApp.controller('moveTaskRuleDefCtrl', function($scope, saveCreateToServer){
$scope.saveCreate=function() {
var reqData = angular.toJson($scope.taskCreateForm);
saveCreateToServer.saveDataToServer(reqData).success(function(data) {
});
}
});
})
在服务器端:
def save(params){
println "<<<<<<<<<uu<<<<<<<<<"+params
}
当您使用$http
和data
键发布数据时,该数据不会作为查询或表单数据发送,而是使用正文参数发送。 因此,您不能使用params
来读取它,而只能使用request.JSON
:
def save() {
Map requestData = request.JSON
println "Data: " + requestData
println "Data: " + requestData.firstName
}
另外,您不必将对象转换为JSON字符串,只需传递数据即可:
$http({
method: "POST",
url: "/save",
data: {firstName: "John", lastName: "Doe"}
});
更新作为表单数据传递的数据
是的,您绝对可以这样做,这样就不必更改服务器端代码。 因此,基本上,Angular默认发送Content-Type
为application/json
数据,因此Grails将其作为request.JSON
接收。 因此,只需将其更改为application/x-www-form-urlencoded
:
$http({
method: "POST",
url: "/save",
data: {firstName: "John", lastName: "Doe"},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
现在,这些参数将在Grails控制器中作为params
提供。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.