如何使用Express返回格式良好的201?
我正在尝试使用DS.RESTAdapter
构建带有ember-cli的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)
我很确定问题是当我在新创建的模型上调用save()
时,它正在发送一个post请求给/ express表示回复:
todosRouter.post('/', function(req, res) { res.status(201).end(); });
这是在Ember中创建todo的创建动作:
actions: createTodo: -> return unless title = @get('newTitle')?.trim() @set('newTitle', '') @store.createRecord('todo', title: title isCompleted: false ).save()
任何帮助将不胜感激。 我是新来表达并且不确定为什么jquery不喜欢它返回的201。
问题是它试图在空白响应上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(); });