问题
I'm trying to build todoMVC with ember-cli using the DS.RESTAdapter
and express to mock out the calls. The issue I'm getting is that when I try to save a new todo I see this error in the console:
SyntaxError: Unexpected end of input
at Object.parse (native)
at jQuery.parseJSON (http://localhost:4200/assets/vendor.js:8717:22)
at ajaxConvert (http://localhost:4200/assets/vendor.js:9043:19)
at done (http://localhost:4200/assets/vendor.js:9461:15)
at XMLHttpRequest.jQuery.ajaxTransport.send.callback (http://localhost:4200/assets/vendor.js:9915:8)
I'm pretty sure the issue is that when I call save()
on the newly created model, it is sending a post request to / which express is replying to with this:
todosRouter.post('/', function(req, res) {
res.status(201).end();
});
Here's the create action in Ember that's creating the todo:
actions:
createTodo: ->
return unless title = @get('newTitle')?.trim()
@set('newTitle', '')
@store.createRecord('todo',
title: title
isCompleted: false
).save()
Any help would be greatly appreciated. I'm new to express and not sure why jquery doesn't like the 201 it is returning.
回答1:
The problem is it's trying to parseJSON
on a blank response. It's effectively doing jQuery.parseJSON('')
- which does produce an error if you try an run it.
To resolve it you could return any string that can be parsed as JSON - e.g. the string null
or empty quotes ""
.
todosRouter.post('/', function(req, res) {
res.send('null');
res.status(201).end();
});
todosRouter.post('/', function(req, res) {
res.send('""');
res.status(201).end();
});
来源:https://stackoverflow.com/questions/29931237/how-can-i-return-a-well-formatted-201-with-express