0

Using Struts2. In my Action I have a List<Person> persons;

In javascript, I have this function:

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. 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]; in javascript, everything is posted fine.

So, is there a way to post JSON objects to a Struts2 action via javascript/jquery?

I should mention that I'm using the struts-jquery plugin, not dojo.

2
  • why don't you use S2 Json plugin which will take care of sending and receiving JSON data between UI and Action classes? Commented Mar 15, 2012 at 1:24
  • How would I use the JSON plugin to send to the action using the struts-jquery plugin? The examples I've seen use dojo, not jquery. Commented Mar 15, 2012 at 15:08

2 Answers 2

1

I don't know anything about struts, but this is how I POST objects with json:

$.ajax({
    type: "POST",
    data: JSON.stringify(SOME_JAVASCRIPT_OBJECT),
    contentType: "application/json; charset=utf-8"
    // etc: etc
});
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.