Javascript 如何使用restanglar发布对象?
我想用Angular JS和Restangular将对象发送到Spring控制器。这就是我努力做到这一点的方式:Javascript 如何使用restanglar发布对象?,javascript,spring,angularjs,controller,restangular,Javascript,Spring,Angularjs,Controller,Restangular,我想用Angular JS和Restangular将对象发送到Spring控制器。这就是我努力做到这一点的方式: var scoreTO = { score: parseFloat($scope.score.score), percentage: parseFloat($scope.score.percentage), date: (new Date()).getTime() } Restangular.one("scores").post(scoreTO); 有一个传
var scoreTO = {
score: parseFloat($scope.score.score),
percentage: parseFloat($scope.score.percentage),
date: (new Date()).getTime()
}
Restangular.one("scores").post(scoreTO);
有一个传输对象:
public class ScoreTO implements Serializable{
private double score;
private double percentage;
private long date;
public ScoreTO(){}
public ScoreTO(double score, double percentage, long date){
this.score = score; this.percentage = percentage; this.date=date;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public double getPercentage() {
return percentage;
}
public void setPercentage(double percentage) {
this.percentage = percentage;
}
public long getDate() {
return date;
}
public void setDate(long date) {
this.date = date;
}
}
下面是我如何在Spring Controller中捕获请求:
@Controller
@RequestMapping(value="/score")
public class ScoreController {
(...)
@RequestMapping(value="/scores", method= RequestMethod.POST)
@ResponseBody
public String saveScore(@RequestBody ScoreTO scoreTO){
scoreService.saveScore(scoreTO.getScore(), scoreTO.getPercentage(),
new Date(scoreTO.getDate()));
return "";
}
}
但如果我尝试发送它,我在浏览器控制台中会出现以下错误:
POSThttp://localhost:8084/wpisywarkaAngular/score/scores/[对象%20Object]404(未找到)
我做错了什么?如果有人能帮助我,我会非常高兴的-提前谢谢你。你必须使用
全部
而不是一个
,另一个解决方案是使用customPOST
Restangular.all("scores").post(scoreTO);
您必须使用
all
而不是one
,另一种解决方案可以是使用customPOST
Restangular.all("scores").post(scoreTO);