我在从jquery ajax提交json数据时遇到了一些问题。我搜索了一些类似的问题,但没有为我工作。
$.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格式:
{name:"value", data:["value1","value2"...]}
弹簧控制器:
@JsonView(Views.Public.class)
@RequestMapping(value = "/save-routes", method = RequestMethod.POST)
public @ResponseBody boolean loadRoutes(@RequestBody Route route) {
//codes
return status;
}
我一直收到这个错误:
POST http://localhost:8181/SYBusWebApp/save-route 400 Bad Request 6ms
路线等级:
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
}
停止课程:
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
}
答案 0 :(得分:0)
您的Route对象似乎与您的JSON有效负载不匹配。
{name:"value", data:["value1","value2"...]}
在你的情况下,我希望你的JSON有效负载中有一个嵌套的Stop对象集合用于routeStops:
{
name:"value",
routeStops:[
{
"name":"test",
"latitude": 3.124
},
....
],
...
}