如何在ExtJs 4中获取REST响应消息?
我正在构建 RESTFul Store 示例 ExtJs 4。当添加或删除请求失败时,我希望我的脚本显示 REST 服务器提供的错误。我已成功获取请求的成功状态(请参阅下面的代码),但如何获取响应中提供的消息?
商店:
var store = Ext.create('Ext.data.Store', {
model: 'Users',
autoLoad: true,
autoSync: true,
proxy: {
type: 'rest',
url: 'test.php',
reader: {
type: 'json',
root: 'data',
model: 'Users'
},
writer: {
type: 'json'
},
afterRequest: function(request, success) {
console.log(success); // either true or false
},
listeners: {
exception: function(proxy, response, options) {
// response contains responseText, which has the message
// but in unparsed Json (see below) - so I think
// there should be a better way to reach it than
// parse it myself
console.log(proxy, response, options);
}
}
}
});
典型的 REST 响应:
"{"success":false,"data":"","message":"VERBOSE ERROR"}"
也许我做错了,所以任何建议都会受到赞赏。
I'm building upon RESTFul Store example of ExtJs 4. I'd like my script to display errors provided by the REST server, when either Add or Delete request fails. I've managed to obtain the success status of a request (see the code below), but how do I reach the message provided with the response?
Store:
var store = Ext.create('Ext.data.Store', {
model: 'Users',
autoLoad: true,
autoSync: true,
proxy: {
type: 'rest',
url: 'test.php',
reader: {
type: 'json',
root: 'data',
model: 'Users'
},
writer: {
type: 'json'
},
afterRequest: function(request, success) {
console.log(success); // either true or false
},
listeners: {
exception: function(proxy, response, options) {
// response contains responseText, which has the message
// but in unparsed Json (see below) - so I think
// there should be a better way to reach it than
// parse it myself
console.log(proxy, response, options);
}
}
}
});
Typical REST response:
"{"success":false,"data":"","message":"VERBOSE ERROR"}"
Perhaps I'm doing it all wrong, so any advice is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您的服务遵循 REST 原则,并使用
2xx
以外的 HTTP 状态代码来表示不成功的操作。但是,对于未返回状态 OK
2xx
的响应,Ext 将不会解析响应正文。在这种情况下,异常/响应对象(传递给“异常”事件侦听器)所提供的只是
response.statusText
中的 HTTP 状态消息。因此,您必须自己将responseText 解析为JSON。这实际上并不是一个问题,因为它可以用一行来完成。
根据您的编码风格,您可能还需要添加一些错误处理和/或区分“预期”和“意外”HTTP 错误状态代码。 (这来自 Ext.data.reader.Json)
此行为的原因可能是因为 REST 代理类不是数据包中的第一类成员。它派生自一个公共基类,该基类还定义了标准 AJAX(或 JsonP)代理的行为,该代理仅将 HTTP 状态代码用于通信通道错误。因此,在这种情况下,他们不期望来自服务器的任何可解析消息。
相反,预计会返回指示应用程序错误的服务器响应,并显示 HTTP 状态“正常”以及问题中发布的 JSON 响应(包含
success:"false"
和message:"[您的错误消息]"
)。有趣的是,REST 服务器可以返回具有非 2xx 状态的响应和具有有效 JSON 响应(在 Ext 术语中)的响应正文,并且 success 属性设置为“true”。异常事件仍然会被触发并且响应正文不会被解析。
这种设置没有多大意义 - 我只是想指出 HTTP 状态代码方面的“成功”与正文中的成功属性之间的区别(前者优先于后者)。
更新
对于更透明的解决方案,您可以扩展(或覆盖)Ext.data.proxy.Rest:这会将成功值从
false
更改为true
,然后调用标准流程响应实施。这将模拟“标准”Ext 行为并解析响应文本。当然,这需要一个标准的 JSON 响应,如原始帖子中所述,success:"false"
(否则失败)。但这还没有经过测试,if 表达式可能应该更聪明。
I assume that your service follows the REST principle and uses HTTP status codes other than
2xx
for unsuccessful operations.However, Ext will not parse the response body for responses that do not return status OK
2xx
.What the exception/response object (that is passed to 'exception' event listeners) does provide in such cases is only the HTTP status message in
response.statusText
.Therefore you will have to parse the responseText to JSON yourself. Which is not really a problem since it can be accomplished with a single line.
Depending on your coding style you might also want to add some error handling and/or distinguish between 'expected' and 'unexpected' HTTP error status codes. (This is from Ext.data.reader.Json)
The reason for this behavior is probably because of the REST proxy class not being a first class member in the data package. It is derived from a common base class that also defines the behavior for the standard AJAX (or JsonP) proxy which use HTTP status codes only for communication channel errors. Hence they don't expect any parsable message from the server in such cases.
Server responses indicating application errors are instead expected to be returned with HTTP status OK, and a JSON response as posted in your question (with
success:"false"
andmessage:"[your error message]"
).Interestingly, a REST server could return a response with a non-2xx status and a response body with a valid JSON response (in Ext terms) and the success property set to 'true'. The exception event would still be fired and the response body not parsed.
This setup doesn't make a lot of sense - I just want to point out the difference between 'success' in terms of HTTP status code compared to the success property in the body (with the first having precedence over the latter).
Update
For a more transparent solution you could extend (or override) Ext.data.proxy.Rest: this will change the success value from
false
totrue
and then call the standard processResponse implementation. This will emulate 'standard' Ext behavior and parse the responseText. Of course this will expect a standard JSON response as outlined in your original post withsuccess:"false"
(or otherwise fail).This is untested though, and the if expression should probably be smarter.