1

I am trying to pass long array from jquery load to spring controller. I have passed string variable succefully but not the array.

javascript:

function ModuleMemberCheckboxPassIds(){
     var memberUserIds = [];
     var memberNames = [];
     $('.membercheckbox:checked').each(function(i){      
         memberUserIds[i] = $(this).val();
     });
     alert(memberUserIds.length);
     $( "#onetextarea" ).load('/assign_task', {"memberUserIds":memberUserIds,  "testdata": "test", });   
}

spring controller:

@RequestMapping(value="/assign_task")
    public String viewAssignTask(Model model, String testdata, HttpServletRequest request){

        if(request.getAttribute("memberUserIds")==null) System.out.println("null"); else System.out.println("not null"); //returns null

        System.out.println("test: " + testdata);//returns a value

        model.addAttribute("transferVO", new TransferVO());
        return "module/view-members-checkbox";
    }

In viewAssignTask method I was able to read tesdata variable that was sent from jquery. But memberUserIds returns null

please help

2 Answers 2

3

Are you expecting an incoming JSON object on the server? If so you could try using $.ajax instead of $.load:

var data={"memberUserIds":memberUserIds,"testdata": "test"};

$.ajax({
    url: '/assign_task',
    type: 'GET',
    data: JSON.stringify(data),
    contentType: 'application/json',
    success: function(html) {
        $("#onetextarea").html(html);
    }
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

GET did not work, i had to change it to post. I don't know why but I had to write this data: JSON.stringify(eval({"memberUserIds":memberUserIds ,"testdata":"test"})),
2

You can use:

request.getParameterValues("memberUserIds");

for getting multivalued Parameters.

Otherwise:

One suggestion you want to use features of Spring MVC then code should be :

RequestMapping(value="/assign_task")
    public String viewAssignTask(Model model, String testdata,@RequestParam String [] memberUserIds, @RequestParam String testData){
//Your Code
 }

3 Comments

request.getParameterValues("memberUserIds"); was still showing null. I don't get it using @RequestParam was not working. Call was not directed to this controller. @RequestMapping(value="/assign_task") public String viewAssignTask(Model model,@RequestParam String[] memberUserIds,@RequestParam String testdata){ ...} Javascript call: $( "#onetextarea" ).load('/hamdan/module/assign_task', {"memberUserIds":memberUserIds, "testdata": "test", });
there is no error message. Code is fine. I think request couldn't find which contoller and path the request should be dispatched
may be URL of the request is wrong. so try to add absolute path load('localhost:8080/myapp_/assign_task'

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.