【发布时间】:2017-04-15 00:05:55
【问题描述】:
我正在尝试通过 POST 请求向使用 Ember Data 2.10 的基于 JSONAPI 的 API 提交无效数据。
API 在响应中使用422 代码和此有效负载正确响应(请注意,这些是error objects,而不是“正常” JSONAPI 有效负载响应):
{
"errors": [{
"title": "Title can't be blank",
"id": "title",
"code": "100",
"source": {
"pointer": "/data/attributes/title"
},
"status": "422"
}, {
"title": "Layout can't be blank",
"id": "layout",
"code": "100",
"source": {
"pointer": "/data/relationships/layout"
},
"status": "422"
}, {
"title": "Page type can't be blank",
"id": "page-type",
"code": "100",
"source": {
"pointer": "/data/attributes/page-type"
},
"status": "422"
}]
}
错误似乎大部分都可以正常加载到模型中,但我在控制台中收到此错误:
ember.debug.js:19160 Error: The adapter rejected the commit because it was invalid
at ErrorClass.EmberError (ember.debug.js:19083)
at ErrorClass.AdapterError (errors.js:23)
at ErrorClass (errors.js:49)
at Class.handleResponse (rest.js:821)
at Class.handleResponse (data-adapter-mixin.js:100)
at Class.superWrapper [as handleResponse] (ember.debug.js:24805)
at ajaxError (rest.js:1342)
at Class.hash.error (rest.js:916)
at fire (jquery.js:3305)
at Object.fireWith [as rejectWith] (jquery.js:3435)
是什么导致了这个错误?服务器返回的 JSON 有效负载有问题吗?最近改变的一件事是引入了指向/data/relationships/layout 的指针; Ember Data 会因此而窒息吗?
我还可能注意到,通过PATCH 请求提交类似的不良数据不会在控制台中触发此错误。
主要问题是这会导致验收测试失败,我似乎找不到解决方法。能够在应用程序中测试此行为会很好,但我现在只需将其注释掉即可。
在更新到 2.10 之前,我也在 Ember Data 2.7 上尝试过这个,看看是否能解决这个问题。两个版本都出现同样的错误。
【问题讨论】:
-
你的json好像不是JSON API 1.0格式
-
@MilkyWayJoe 是什么导致它不符合 JSON API 1.0?
-
格式似乎不正确。你必须让你的 Rails 发送类似于
"data": [{ "type": "error", "id": "title", "attributes": { "title": "Title can't be blank", "code": "100" }, {"type": "error" ...的东西等等。注意到结构的不同了吗? -
@MilkyWayJoe 我遇到的问题是错误对象,它们是另一种动物。 jsonapi.org/examples/#error-objects
-
另外,适配器不应该正确拒绝无效的 POST 吗?你确定它不只是告诉你它正在正确地完成它的工作吗?在非开发环境中你会得到什么?
标签: ember.js ember-data json-api