如何使用Express返回格式良好的201?

spi*_*ock 2 jquery express ember.js ember-cli

我正在尝试用ember-cli构建todoMVC,使用DS.RESTAdapter和表达来模拟调用.我得到的问题是,当我尝试保存新的待办事项时,我在控制台中看到此错误:

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)
Run Code Online (Sandbox Code Playgroud)

我很确定问题是,当我调用save()新创建的模型时,它正在发送一个发布请求给/快递正在回复:

 todosRouter.post('/', function(req, res) {
    res.status(201).end();
  });
Run Code Online (Sandbox Code Playgroud)

这是在Ember中创建todo的创建动作:

actions:
    createTodo: ->
      return unless title = @get('newTitle')?.trim()

      @set('newTitle', '')
      @store.createRecord('todo',
        title: title
        isCompleted: false
      ).save()
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.我是新来表达并且不确定为什么jquery不喜欢它返回的201.

jmu*_*yau 6

问题是它试图做出parseJSON空白回应.它正在有效地执行jQuery.parseJSON('')- 如果您尝试运行它会产生错误.

要解决它,您可以返回任何可以解析为JSON的字符串 - 例如字符串null或空引号"".

todosRouter.post('/', function(req, res) {
  res.send('null');
  res.status(201).end();
});

todosRouter.post('/', function(req, res) {
  res.send('""');
  res.status(201).end();
});
Run Code Online (Sandbox Code Playgroud)