backbone.js模型保存(PUT请求)jquery错误
我使用 model.save() 方法保存backbone.js 模型,该方法向服务器发出 PUT 请求。 服务器认为没有问题,只是设置响应的状态代码。 到目前为止一切顺利,但是当请求返回(即 8)时,我在 jquery 代码中的某处收到“null is null or not an object error”。 我猜 jquery 期望从 PUT 请求中返回一些内容。 当我尝试将“ok”打印到服务器上的响应流时,它没有在浏览器中引发异常,而是触发了 model.save() error() 回调函数。 有人知道这里发生了什么事吗?
I'm saving a backbone.js model using the model.save() method, which fires a PUT request to the server.
The server sees it fine and just sets the status code for response.
So far so good but when the request returns (to ie 8) I get a 'null is null or not an object error' somewhere in jquery code.
I guess jquery expects something to come back from a PUT a request.
When I tried just printing 'ok' to the response stream on the server it didn't throw an exception in the browser but triggered the model.save() error() callback function.
Anyone knows what's happening here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。如果您只是返回 200 的标头,那么您做得还不够。
你需要把一些东西归还给身体。 Backbone 期望它是您对象的 JSON。在创建的情况下,这是必要的,因为您至少需要返回对象的 ID,以便 Backbone 知道如何更新。在更新的情况下,约定是(至少)返回更改的内容。
在任何一种情况下,您都可以返回整个对象...这是最简单的,我更喜欢这种方式,因为它可以确保与服务器的一致性。
我想您可能可以只返回
{}
,这对于更新来说可能没问题,但对于创建来说就不够了。我坚持把物体放回体内。
Yes. If you are simply returning a header with 200, you aren't doing enough.
You need to return something in the body. Backbone expects it to be the JSON of your object. In the case of a create, this is necessary because you at least need to return the ID of your object so that Backbone knows how to update. In the case of an update, the convention is to (at least) return the things that changed.
In either case, you can just return the entire object... that is what is easiest and I prefer it that way because it ensures consistency with the server.
I suppose you can probably just return
{}
, and that might be fine for updates, but it would be insufficient for creates.I'd stick with returning the object in the body.