[英]How to post an array of JSON objects to a Struts2 action using jQuery?
Using Struts2. 使用Struts2。 In my Action I have a
List<Person> persons;
在我的动作中,我有一个
List<Person> persons;
In javascript, I have this function: 在javascript中,我具有以下功能:
function mytestfunction() {
var url = "MyAction_mytestfunction.action";
var params = {};
var arr = [];
var p1 = { "firstname" : "John", "lastname" : "Doe"};
var p2 = { "firstname" : "Rob", "lastname" : "Smith"};
arr.push(p1); arr.push(p2);
params["persons"] = arr;
$.post(url, params, function(data) {
console.log("done");
});
}
Problem is, the post() never reaches the action. 问题是,post()永远不会到达动作。 There are no errors in the logs, nothing.
日志中没有错误,没有任何错误。
This all changes if instead of posting objects I post primitives. 如果不是我发布原始对象而不是发布对象,这一切都会改变。 So when I have a
List<Integer> nums
in the Action and params["nums"] = [1,2,3];
因此,当我在Action中有一个
List<Integer> nums
并且params["nums"] = [1,2,3];
in javascript, everything is posted fine. 在javascript中,一切都很好发布。
So, is there a way to post JSON objects to a Struts2 action via javascript/jquery? 因此,有没有一种方法可以通过javascript / jquery将JSON对象发布到Struts2操作中?
I should mention that I'm using the struts-jquery plugin, not dojo. 我应该提到我使用的是struts-jquery插件,而不是dojo。
I don't know anything about struts, but this is how I POST objects with json: 我对struts一无所知,但这就是我用json发布对象的方式:
$.ajax({
type: "POST",
data: JSON.stringify(SOME_JAVASCRIPT_OBJECT),
contentType: "application/json; charset=utf-8"
// etc: etc
});
you can try as following example: 您可以尝试以下示例:
$.ajax({
type: "Post",
url: "action_name", //you can pass through querystring like actionname?paramname=value
data : {
//here comes your param or array
},
dataType:"json",
contentType: "application/json; charset=utf-8",
});
If you want to pass through querystring type must be GET 如果要通过查询字符串,则类型必须为GET
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.