快递如何返回格式良好的201? [英] How can I return a well formatted 201 with Express?
问题描述
我正在使用 DS.RESTAdapter
使用ember-cli构建todoMVC,并表达以嘲笑呼叫。我得到的问题是,当我尝试保存一个新的todo我在控制台中看到这个错误:
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)
$ b $我很确定的问题是,当我在新创建的模型上调用 save()
时,它正在向/正在回复的发送请求这样:
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();
});
以下是Ember中创建todo的创建操作:
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()
任何帮助将不胜感激。我是新来表达,不知道为什么jquery不喜欢201它正在返回。
Any help would be greatly appreciated. I'm new to express and not sure why jquery doesn't like the 201 it is returning.
推荐答案
问题是它是尝试以 parseJSON
为空白的回复。它有效地执行 jQuery.parseJSON('')
- 如果您尝试运行它会产生错误。
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.
要解决它,您可以返回任何可以解析为JSON的字符串 - 例如字符串 null
或空引号。
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();
});
这篇关于快递如何返回格式良好的201?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!