简体   繁体   中英

400 bad request while sending data with jquery ajax in spring framework?

I am having some issue submitting json data from jquery ajax. I have googled some similar problems but non of them worked for me.

            $.ajax({
                type : "POST",
                contentType : "application/json",
                url : "save-routes",
                data : JSON.stringify(routeObject),
                dataType : 'json',
                timeout : 100000,
                success : function(status) {
                    console.log("SUCCESS ADDING ROUTE DATA");
                    return status;
                },
                error : function(e) {
                    console.log("ERROR WHILE ADDING ROUTE DATA");
                    return false;
                },
                done : function(e) {
                    console.log("DONE");
                    //return true;
                }
            });

routeObejct Json format:

{name:"value", data:["value1","value2"...]}

spring controller:

    @JsonView(Views.Public.class)
    @RequestMapping(value = "/save-routes", method = RequestMethod.POST)
    public @ResponseBody boolean loadRoutes(@RequestBody Route route) {
        //codes
        return status;
    }

I keep getting this error:

POST http://localhost:8181/SYBusWebApp/save-route 400 Bad Request 6ms

Route Class:

public class Route {

    @JsonView(Views.Public.class)
    private String name;

    @JsonView(Views.Public.class)
    private ArrayList<stop> routeStops;

    private String updatedRouteName;
    private ArrayList<String> addedRouteStopNames;

    //getters and setters
}

Stop Class:

public class Stop {

    @JsonView(Views.Public.class)
    private String name;

    @JsonView(Views.Public.class)
    private float latitude;

    @JsonView(Views.Public.class)
    private float longitude;

    private String updatedName; 

//getters and setters
}

It seems your Route object is not matching your JSON payload.

{name:"value", data:["value1","value2"...]}

In your case I would expect a nested collection of Stop objects within your JSON payload for routeStops:

{
   name:"value", 
   routeStops:[
       {
         "name":"test",
         "latitude": 3.124
       },
       ....
   ],
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM