Ember save() throws JSON.parse error: "unexpected end of data at line 1 column 1 of the JSON data"
Ember save() throws JSON.parse error: "unexpected end of data at line 1 column 1 of the JSON data"
我通过路由和控制器创建并保存了一个模型记录。当我通过控制器(通过 savePlace 操作)保存记录时,我在 JS 控制台中看到此错误:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
我试过不在模型上设置任何内容以及在模型上设置虚拟数据,但我得到了同样的错误。我也是用户 ember-cli http-mocks 作为处理 JSON 响应的测试后端。我意识到这可能是响应,但我不确定如何配置响应。
相关代码如下:
routes/places/new.js:
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.createRecord('place');
},
});
controllers/places/new.js:
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
saveGeom(geom) {
this.get('model').set('geometry', geom);
},
savePlace(data) {
this.get('model').set('name', this.get('name')).set('description', this.get('description'));
this.get('model').save().then(function() {
alert("SUCCESS");
}, function(error) {
console.log(error);
});
}
}
});
server/mocks/place.js:
placeRouter.post('/places', function(req, res) {
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.send({
"places": {
id: 1,
name: "Triangle",
description: "Ryan Christiani",
geometry: {
"type": "Polygon",
"coordinates": [
[
[-84.32281494140625,34.9895035675793],
[-81.73690795898438,36.41354670392876],
[-83.616943359375, 34.99850370014629],
[-84.05639648437499,34.985003130171066],
[-84.22119140625, 34.985003130171066],
[-84.32281494140625,34.9895035675793]
]
]
}
}
});
});
谢谢!
我认为您在 JSON 对象的错误位置使用了错误的括号。
查看此页面
http://www.tutorialspoint.com/json/json_syntax.htm
http-mocks 配置错误。应该是下面的代码片段。服务器用一组对象作为响应('GET /' 的响应)。不确定为什么会触发 JSON.parse 错误,但这是正确的配置。
placeRouter.post('/', function(req, res) {
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.send({
'places': [
{
id: 1,
name: "Using Ember CLI to create a Fixture Adapter.",
description: "Ryan Christiani",
geometry: {
"type": "Polygon",
"coordinates": [
[
[-84.32281494140625,34.9895035675793],
[-81.73690795898438,36.41354670392876],
[-83.616943359375, 34.99850370014629],
[-84.05639648437499,34.985003130171066],
[-84.22119140625, 34.985003130171066],
[-84.32281494140625,34.9895035675793]
]
]
}
}]});
});
我通过路由和控制器创建并保存了一个模型记录。当我通过控制器(通过 savePlace 操作)保存记录时,我在 JS 控制台中看到此错误:
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
我试过不在模型上设置任何内容以及在模型上设置虚拟数据,但我得到了同样的错误。我也是用户 ember-cli http-mocks 作为处理 JSON 响应的测试后端。我意识到这可能是响应,但我不确定如何配置响应。
相关代码如下:
routes/places/new.js:
import Ember from 'ember';
export default Ember.Route.extend({
model: function() {
return this.store.createRecord('place');
},
});
controllers/places/new.js:
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
saveGeom(geom) {
this.get('model').set('geometry', geom);
},
savePlace(data) {
this.get('model').set('name', this.get('name')).set('description', this.get('description'));
this.get('model').save().then(function() {
alert("SUCCESS");
}, function(error) {
console.log(error);
});
}
}
});
server/mocks/place.js:
placeRouter.post('/places', function(req, res) {
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.send({
"places": {
id: 1,
name: "Triangle",
description: "Ryan Christiani",
geometry: {
"type": "Polygon",
"coordinates": [
[
[-84.32281494140625,34.9895035675793],
[-81.73690795898438,36.41354670392876],
[-83.616943359375, 34.99850370014629],
[-84.05639648437499,34.985003130171066],
[-84.22119140625, 34.985003130171066],
[-84.32281494140625,34.9895035675793]
]
]
}
}
});
});
谢谢!
我认为您在 JSON 对象的错误位置使用了错误的括号。 查看此页面 http://www.tutorialspoint.com/json/json_syntax.htm
http-mocks 配置错误。应该是下面的代码片段。服务器用一组对象作为响应('GET /' 的响应)。不确定为什么会触发 JSON.parse 错误,但这是正确的配置。
placeRouter.post('/', function(req, res) {
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.send({
'places': [
{
id: 1,
name: "Using Ember CLI to create a Fixture Adapter.",
description: "Ryan Christiani",
geometry: {
"type": "Polygon",
"coordinates": [
[
[-84.32281494140625,34.9895035675793],
[-81.73690795898438,36.41354670392876],
[-83.616943359375, 34.99850370014629],
[-84.05639648437499,34.985003130171066],
[-84.22119140625, 34.985003130171066],
[-84.32281494140625,34.9895035675793]
]
]
}
}]});
});