backbonejs模型如何处理服务器端错误?
backbonejs模型如何处理服务器端错误?
当我发送带有错误数组的 json 时,我仍然会收到一个成功回调,响应参数内包含错误数组。
How backbonejs models handle server side errors?
When Im sending json with errors array, im still get a success callback with errors array inside response parameter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在我的客户端代码中,我检查错误属性是否存在并根据需要做出反应。
例如,我使用的是 Collection.create 函数,如果请求成功,它会调用 Collection 的 add 函数。因此,我重写了 Collection 的 add 函数,以防止在模型具有“errors”属性并且不调用“super”方法时添加模型。
让我的应用程序返回不成功状态代码也可以,因为它可以防止运行成功回调。但是,我不喜欢仅仅因为提交无效就返回 400 错误的想法。这是我在非 Backbone.js 应用程序中从未做过的事情。 (“无脊椎动物”?)
4XX 状态代码的描述似乎都与验证失败的概念不符。 400 和 403 很接近,但仍然看起来好像它们是用于其他用途。另一方面,用户实际上并不关心你返回什么状态代码;也可能是 404。
这实际上是您想要在服务器端还是客户端编写更多代码的问题。我的应用程序或多或少会忽略验证结果,并返回序列化为 JSON 的记录,无论是否保存,因此我选择从这个角度进行工作。
In my client-side code I check for the presence of the errors attribute and react as necessary.
For example, I was using the Collection.create function, which calls the add function of the Collection if the request was successful. So, I overrode my Collection's add function to prevent the model from being added if it has an 'errors' attribute, and if it doesn't call the "super" method.
Having my app return a non-success status code would work too, as it prevents success callbacks from being run. But, I didn't like the idea of returning a 400 error just because the submission was invalid. That's something I've never had to do in non-Backbone.js apps. ("Invertebrates"?)
None of the descriptions for 4XX status codes seemed to really match the concept of failed validation. 400 and 403 come close, but still come off as if they were intended for other uses. On the other hand, the user really doesn't care what status code you return; might as well be a 404.
It's really a matter of whether you want to write more code server-side or client-side. My application more or less ignores the outcome of validation and returns the record serialized to JSON whether it was saved or not, so I chose to work from that angle.
服务器端骨干验证:
服务器端
返回错误数组
客户端
调用 model.save 时传递“wait: true”作为选项:
更新模型的验证函数以检查错误array:
结果
如果服务器重新运行 [错误],错误回调将运行,并且模型不会“更改”。
更新:
从版本 0.9.10 开始,此功能将不再有效。您应该使用“invalid”事件或使用
model.validate(attributes, options)
函数的新options
参数:if (typeof(options.错误 === '函数')) options.error();
Server side backbone validation:
Server side
Return errors array
Client side
Pass "wait: true" as option when calling model.save:
Update the model's validate function to check for the errors array:
Outcome
The error callback will run and the model will not "change" if the server retruns [errors].
UPDATE:
As of version 0.9.10 this will no longer work. You should either use the "invalid" event or use the new
options
argument of themodel.validate(attributes, options)
function:if (typeof(options.error === 'function')) options.error();
或者也许我应该使用 http 状态。目前我选择了 403 状态来指示服务器端的验证错误。有关状态的更多信息可以在这里找到:http://restpatterns.org/HTTP_Status_Codes
Or may be I should use http status. Currently I have chosen 403 status to indicate validation errors on server side. More info about statuses could be found here: http://restpatterns.org/HTTP_Status_Codes
Backbone.js 通过您现有的任何底层库路由其 ajax 调用; JQuery 或 Zepto。所以基本上,这个库决定什么构成成功,什么构成错误。
听起来您的服务器可能会返回 403 状态代码,但这被解释为成功返回。因此,您的成功回调将被调用。就其价值而言,403 状态代码对于返回错误似乎很奇怪,除非这些错误与授权相关。
这是您要找的吗?
Backbone.js routes its ajax calls through whatever underlying library you have in place; JQuery or Zepto. So basically, this library decides what constitutes success and what constitutes error.
It sounds like your server may be returning a 403 status code, but that is being interpreted as a success return. Thus, your success callback is being called. For what its worth, a 403 status code seems odd for returning errors unless these errors are authorization-related.
Is this what you were looking for?
我将讨论使用 jQuery 作为底层 js 库(尽管我假设 zepto 的工作方式相同)。 Backbone 通过 Backbone.sync() 路由异步事件,例如 model.save()、collection.fetch() 等。该函数委托 $.ajax() 来执行实际的 ajax 调用。因此,只要响应的标头表示错误(例如 4XX 标头),您在创建对象时指定的任何错误函数都将被使用。
通常您会看到 success 函数中完成了错误处理,因为返回实际上并不意味着 jquery 出现错误(当响应标头为 2XX 时)。
I'll speak to using jQuery as your underlying js library (though I assume zepto works the same way). Backbone routes async events like model.save() collection.fetch(), etc...through Backbone.sync(). This function delegates to $.ajax() to do the actual ajax call. So whatever error function you've specified when you created you object will be used AS LONG AS the header of the response signifies an error, like a 4XX header.
Often you will see error handling done in the success function, because the return does not actually signify an error to jquery (when the response header is 2XX).
我通常从服务器返回一个 HTTP 错误以及 JSON 错误描述,
像这样的东西:
I normally return an HTTP Error from the server along with a JSON Error description,
something like this: