rest - 如何在 ExtJs 4 中获取 REST 响应消息?

标签 rest extjs extjs4

我正在建立 RESTFul Store example 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"}"

也许我做错了,所以任何建议都值得赞赏。

最佳答案

我假设您的服务遵循 REST 原则并使用 2xx 以外的 HTTP 状态代码对于不成功的操作。
但是,Ext 不会为不返回状态 OK 2xx 的响应解析响应正文。 .
在这种情况下,异常/响应对象(传递给“异常”事件监听器)提供的只是 response.statusText 中的 HTTP 状态消息。 .

因此,您必须自己将 responseText 解析为 JSON。这不是一个真正的问题,因为它可以用一条线来完成。

var data = Ext.decode(response.responseText);

根据您的编码风格,您可能还想添加一些错误处理和/或区分“预期”和“意外”HTTP 错误状态代码。 (这是来自 Ext.data.reader.Json)
getResponseData: function(response) {
    try {
        var data = Ext.decode(response.responseText);
    }
    catch (ex) {
        Ext.Error.raise({
            response: response,
            json: response.responseText,
            parseError: ex,
            msg: 'Unable to parse the JSON returned by the server: ' + ex.toString()
        });
    }

    return data;
},

这种行为的原因可能是因为 REST 代理类不是数据包中的第一类成员。它派生自一个公共(public)基类,该基类还定义了标准 AJAX(或 JsonP)代理的行为,该代理仅将 HTTP 状态代码用于通信 channel 错误。因此,在这种情况下,他们不期望来自服务器的任何可解析消息。
指示应用程序错误的服务器响应应返回 HTTP 状态 OK,以及您的问题中发布的 JSON 响应(带有 success:"false"message:"[your error message]" )。

有趣的是,一个 REST 服务器可以返回一个具有非 2xx 状态的响应和一个具有有效 JSON 响应(在 Ext 术语中)的响应主体,并且成功属性设置为“真”。异常事件仍然会被触发并且响应正文不会被解析。
这种设置没有多大意义 - 我只想指出 HTTP 状态代码中的“成功”与正文中的成功属性之间的区别(第一个优先于后者)。

更新

对于更透明的解决方案,您可以扩展(或覆盖)Ext.data.proxy.Rest:这将更改成功值 falsetrue然后调用标准的 processResponse 实现。这将模拟“标准”Ext 行为并解析 responseText。当然,这将需要一个标准的 JSON 响应,如您在原始帖子中概述的 success:"false"。 (或以其他方式失败)。
虽然这是未经测试的,if 表达式可能应该更聪明。
Ext.define('Ext.ux.data.proxy.Rest', {
    extend: 'Ext.data.proxy.Rest',

    processResponse: function(success, operation, request, response, callback, scope){
        if(!success && typeof response.responseText === 'string') { // we could do a regex match here
            var args = Array.prototype.slice.call(arguments);
            args[0] = true;
            this.callParent(args);
        } else {
            this.callParent(arguments);
        }
    }
})

关于rest - 如何在 ExtJs 4 中获取 REST 响应消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7546469/

相关文章:

c# - Rally C# : Is it possible to create a new user story along with an attachment?(避免查询用户故事引用)

REST API - URL 中的字符串或数字标识符

javascript - 使用来自 Neo4j 服务器的 JSON 数据在 Angular 中创建工厂

grid - 在网格上的 selectionchange 上显示不同的项目

android - 使用 Retrofit 进行两次 API 调用以形成一个对象

CSS 样式未应用于 Highchart 中的系列

ExtJS - 使用 JsonReader 对复杂对象进行 null 安全检索

javascript - 扩展 : Redirecting to other page

java - 如何使用 Java Servlet 将图像从 Mongodb 发送到 Nextjs

extjs4 - 去除面板中的白色背景