6

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 Answer 1

9

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();
});
2
  • Thanks! That was exactly the issue. jQuery was expecting a response body and wasn't getting one.
    – spinlock
    Apr 29, 2015 at 17:45
  • 1
    I believe the end() shouldn't be necessary as send() is used. It's either one or the other, not both AFAIK.
    – marksyzm
    Jan 29, 2019 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.