【问题标题】:How can I get content type from response when there is a proxy ajax call in extjs?当 extjs 中有代理 ajax 调用时,如何从响应中获取内容类型?
【发布时间】:2020-09-06 09:31:42
【问题描述】:

在下面的代码中,我对服务器进行了代理 ajax 调用。当会话处于活动状态时,我会收到预期的 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】:

    恕我直言,最好检查代理的exception 事件处理程序中的http response status code。像这样的:

    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.
                };
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-02
      • 2021-08-22
      • 1970-01-01
      • 2012-09-27
      • 2011-07-18
      • 1970-01-01
      • 2011-02-25
      • 2019-04-09
      相关资源
      最近更新 更多