骨干-处理来自服务器的空响应 [英] Backbone - Handle empty response from the server
问题描述
我使用访存从服务器检索我的模型.当我的数据库中有数据但它如何处理服务器发送给客户端的空响应时,它的效果很好.
I use fetch to retrieve my models from the server. It works great when I have data in my DB but how to handle an empty response send by the server to the client ?
例如,如果用户尚未保存任何数据,则服务器发送带有空数组的http 200响应,而主干会触发错误回调,但我只想通知用户该数据库中没有保存任何数据.在这种情况下,如果响应为空,则意味着没有要加载的模型,我也不想使用响应创建任何模型.
For example if no data have been already saved by the user the server send a http 200 response with an empty array and backbone triggers an error callback but I just want to inform the user that there is no data saved in DB. In this case an empty response just means there is no model to load and I don't want to create any model with the response.
这是我使用的代码:
app.plans.fetch({
success: function(data) {
app.Notifications.updateMessages({text: "Plans loaded."});
},
error: function (){
app.Notifications.updateMessages({text: "Error."});
}
});
服务器如何指示没问题,但是没有数据?
How the server can indicate that it's all right but there is just no data ?
推荐答案
如果服务器响应200,则不应触发错误回调.有点奇怪. 您能否添加服务器响应的示例?
If server responds with the 200 it shouldn't trigger an error callback. It's weird. Could you please add the example of the server response?
成功和错误回调还采用以下选项作为参数:(集合,响应,选项)
Also success and error callbacks take the following options as arguments: (collection, response, options)
这篇关于骨干-处理来自服务器的空响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!