在extjs中,当有代理ajax调用时,如何从响应中获取内容类型?

问题描述 投票:0回答:1

在下面的代码中,我对服务器进行了代理ajax调用,当会话处于活动状态时,我收到了预期的json格式,但当会话处于非活动状态时,我收到了作为响应的html文件。当会话处于活动状态时,我收到了预期的json格式,但当会话处于非活动状态时,我收到了一个html文件作为响应。

Ext.define('ExtDashboard.model.data', {
    extend: 'Ext.data.Model',
    fields: [],
    proxy: {
        type: 'ajax',
        url : 'users.json',
        reader: {
            type: 'json',
            rootProperty: 'root',
            success : 'success',
            transform: function(data) {
                   //perform operations on data
                   return data;   
                }
            }
        }
    });

响应的html会有一些文本,显示会话为非活动状态。所以我从html中读取字符串,并做进一步的操作,如重定向到登录页面。但我不知道如何在这里获得响应并提取内容类型,特别是当收到的响应是html而不是json时。

ajax extjs response extjs5 javascript-framework
1个回答
3
投票

IMHO最好检查一下 http响应状态码 在代理的 例外 事件处理程序。类似这样

proxy: {
    type: 'ajax',
    url: '/data.json',
    reader: {
        type: 'json',
        rootProperty: 'data'
    },
    listeners: {
        exception: function (proxy, response, operation) {
            // 401 - Unauthorized
            // 440 - Login Time-out

            if([401, 440].indexOf(response.status) > -1) {
                // Do something i.e. redirect to login page.
            };
        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.