Javascript SyntaxError:每个POST请求都有意外的标记O,而不是解析
我使用Angular和ES6将数据发送到Spring Boot RESTful APIJavascript SyntaxError:每个POST请求都有意外的标记O,而不是解析,javascript,angularjs,json,Javascript,Angularjs,Json,我使用Angular和ES6将数据发送到Spring Boot RESTful API class PostsService { constructor($http) { this.getAllPosts = () => { return $http.get('http://localhost:8080/posts'); }; this.addPost = () => { le
class PostsService {
constructor($http) {
this.getAllPosts = () => {
return $http.get('http://localhost:8080/posts');
};
this.addPost = () => {
let data = {content : "BBB", date : "55.55.5555"};
return $http.post('http://localhost:8080/newPost', data);
};
}
}
PostsService.$inject = ['$http'];
export default angular
.module('blog.Posts')
.service('PostsService', PostsService);
GET请求工作没有任何问题
POST完全发送数据(REST API获取数据并将其放入数据库中,正如它应该的那样),另一方面,生成了这个愚蠢且完全奇怪的错误:
SyntaxError: Unexpected token O
at Object.parse (native)
at fromJson (http://localhost:4000/build/bundle.js:1351:15)
at defaultHttpResponseTransform (http://localhost:4000/build/bundle.js:10202:17)
at http://localhost:4000/build/bundle.js:10293:13
at forEach (http://localhost:4000/build/bundle.js:390:21)
at transformData (http://localhost:4000/build/bundle.js:10292:4)
at transformResponse (http://localhost:4000/build/bundle.js:11065:22)
at processQueue (http://localhost:4000/build/bundle.js:15621:29)
at http://localhost:4000/build/bundle.js:15637:28
at Scope.$eval (http://localhost:4000/build/bundle.js:16889:29)
我想值得指出的是,我使用的是webpack,没有进行任何JSON解析(不包括角度http解析,我没有相关信息)。问题是——不知何故,后端需要返回JSON。所以我改变了:
@RequestMapping(path="/newPost", method= RequestMethod.POST)
public @ResponseBody() String createPost(@RequestBody Post payload) {
repository.save(payload);
return "OK";
}
致:
@RequestMapping(path=“/newPost”,method=RequestMethod.POST)
public@ResponseBody()列表createPost(@RequestBody-Post-payload){
保存(有效载荷);
列表=新的ArrayList();
列表。添加(“确定”);
退货清单;
}
它成功了。干杯 听起来服务器上的响应不正确。请进入浏览器开发人员工具并从响应中复制JSON。看来你们的后端没有返回格式良好的jsonHoly烟,伙计们,非常感谢。我从后端返回了ArrayList而不是字符串,一切都开始工作了。干杯
@RequestMapping(path="/newPost", method= RequestMethod.POST)
public @ResponseBody() List<String> createPost(@RequestBody Post payload) {
repository.save(payload);
List<String> list = new ArrayList<String>();
list.add("OK");
return list;
}