Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending javascript object arrays as parameters to controller

Question is pretty self explanatory. I want to send 2 different arrays of objects through a POST form without ajax to my controller.

I changed my question to using ajax and using a get request due to the size of the params. Currently getting a 400 (Bad Request). I have no idea why. Please take a look...

I have objects:

var phone = {phoneId:"", phoneNumber:"", phoneType:""};
var schedule = {scheduleId:"", time:"", day:""};

Which I place into a javascript arrays:

var phones = [phone1, phone2, phone3];
var schedules = [schedule1, schedule2];

and I use ajax to send:

var data = {
    index: id,
    schedules: schedules,
    phones: phones
}
var url = "/myController/myUrl"

$.getJSON(url, data, function(result){
    if(result.ok){
         $('#messageAlertSuccess').show();
    } else {
         $('#messageAlertError').show();    
    }
});

I created wrapping classes to map them like so:

public class PhoneWrapper(){
    private String phoneId;
    private String phoneNumber;
    private String phoneType;
}

And of course the scheduleWrapper follows the same convention.

Here's the method in my controller:

@ResponseBody
@RequestMapping(value="/myUrl", method=RequestMethod.GET)
public Result doSomething(@RequestParam("index") int index,
                          @RequestParam("phones") Set<PhoneWrapper> phoneWrappers,
                          @RequestParam("schedules") Set<ScheduleWrapper> scheduleWrappers,
                          Model model,
                          HttpSession session){

         //do stuff here.

}

I am currently getting a 400. So what's wrong?


Update: here's the url that the .getJSON jquery method is building:

http://localhost:8080/myApp/myController/myUrl?index=9&schedules%5B0%5D%5BscheduleId%5D=1&schedules%5B0%5D%5BfromDay%5D=Monday&schedules%5B0%5D%5BtoDay%5D=Friday&schedules%5B0%5D%5BfromTime%5D=08%3A30%3A00&schedules%5B0%5D%5BtoTime%5D=16%3A00%3A00&schedules%5B1%5D%5BscheduleId%5D=5&schedules%5B1%5D%5BfromDay%5D=Saturday&schedules%5B1%5D%5BtoDay%5D=Monday&schedules%5B1%5D%5BfromTime%5D=09%3A00%3A00&schedules%5B1%5D%5BtoTime%5D=13%3A00%3A00&phones%5B0%5D%5BphoneId%5D=6&phones%5B0%5D%5BphoneNumber%5D=787-788-1111&phones%5B0%5D%5BphoneType%5D=PHONE&phones%5B1%5D%5BphoneId%5D=106&phones%5B1%5D%5BphoneNumber%5D=787-795-4095&phones%5B1%5D%5BphoneType%5D=FAX
like image 951
Nimchip Avatar asked Jul 24 '13 19:07

Nimchip


1 Answers

I see a few things that don't look right

unless you have getters and setters in your wrappers (DTO is a better name), i don't use them for my DTOs for xhr calls, you need to change

public class PhoneWrapper(){
    private String phoneId;
    private String phoneNumber;
    private String phoneType;
}

to have public fields vs private

public class PhoneWrapper(){
    public String phoneId;
    public String phoneNumber;
    public String phoneType;
}

Your js arrays are not arrays but objects;

var phones = {phone1, phone2, phone3};
var schedules = {schedule1, schedule2};

Here they are as arrays

var phones = [phone1, phone2, phone3];
var schedules = [schedule1, schedule2];

Make sure you naming is the same of both the js and java sides. I find it very helpful to turn on the debugging when troubleshooting these problems. log4j -

<logger name="org.springframework.web.servlet.mvc" >
    <level value="debug" />
</logger>

EDIT

So after the question was updated with more info I notice that it was the same problem as Binding a list in @RequestParam

like image 125
denov Avatar answered Sep 22 '22 16:09

denov