使用Flash(Flax)post方式跨越请求的Extjs4代理(Proxy)的实现类

基于变态的需求,远程服务器只接受post方式数据,所以不能用extjs的jsonp方式去跨域请求数据,就只好用flash的方式了,当然,这也需要服务器端的支持.

代码如下:

/**
 * 使用Flash方式进行Post跨越调用的Proxy类
 */
Ext.define('GBox.data.PostpProxy', {
    requires:['Ext.util.MixedCollection'],

    extend:'Ext.data.proxy.Server',

    // 定义代理别名,很重要

    alias:'proxy.postp',
    doRequest:function (operation, callback, scope) {
        var writer = this.getWriter(),
            request = this.buildRequest(operation, callback, scope);
        if (operation.allowWrite()) {
            request = writer.write(request);
        }
        /**
         * 调用父类构造回调函数
         * @type {*}
         */
        var extCB = this.createRequestCallback(request, operation, callback, scope);
        /**
         * 由于自定义的flash跨越回调方法的参数与ext的不同,所以在这组装一下
         * @param data
         */
        var successCallback = function (data) {
            extCB(request, true, {
                request:request,
                requestId:1,
                responseText:data,
                responseXML:null,
                status:200,
                statusText:'ok',
                getAllResponseHeaders:Ext.emptyFn,
                getResponseHeader:Ext.emptyFn
            });
        };
        var timeoutCallback = function (data) {
            Ext.MessageBox.alert('出错了','请求超时!');

        };

       var errorCallback = function (data) {

            Ext.MessageBox.alert('出错了','服务器错误!');

        };

        var params = request.params;
        var strParams = "";
        for (var p in params) {
            strParams += p + "=" + params[p] + "&";
        }
        if ("" != strParams) {
            strParams = strParams.substring(0, strParams.length - 1);
        }
        /**
         * 调用自定义的flash跨越请求方法
         */
        pro.net.http.postp(
            request.url,
            strParams,
            successCallback,
            {
                errorFn:errorCallback,
                timeoutFn:timeoutCallback
            }
        );
        return request;
    },
    createRequestCallback:function (request, operation, callback, scope) {
        var me = this;
        return function (options, success, response) {
            me.processResponse(success, operation, request, response, callback, scope);
        };
    }
});


使用方法
与使用其它ajax的代理方式完全一样


 Ext.define('User', {
                extend:'Ext.data.Model',
                fields:[
                    {name:'employeeId', type:'string'},
                    {name:'systemLevel', type:'int'},
                    {name:'email', type:'string'},
                    {name:'employeeName', type:'string'},
                    {name:'area', type:'string'}
                ]
            });
            var postpStore = new Ext.data.Store({
                autoLoad:false,
                model:'User',
                pageSize:10,
                proxy:{
                    // 这儿就是使用我们自定义的代理了
                    type:'postp',

                    url:'http://172.17.**.**/get?pid=2001&enc=utf-8&res=json',
                    reader:{
                        type:'json',
                        root:'data.result',
                        totalProperty:'page.total'
                    }
                }
            });
            var grid = Ext.create('Ext.grid.Panel', {
                title:'用户',
                store:postpStore,
                columns:[
                    { header:'employeeId', dataIndex:'employeeId' },
                    { header:'systemLevel', dataIndex:'systemLevel'},
                    { header:'employeeName', dataIndex:'employeeName'},
                    { header:'area', dataIndex:'area'},
                    { header:'email', dataIndex:'email' }
                ],
                dockedItems:[
                    {
                        xtype:'toolbar',
                        items:[
                            {
                                text:'查询',
                                iconCls:'icon-search',
                                handler:function () {
                                    Ext.apply(postpStore.proxy.extraParams, {
                                        modelName:1
                                    });
                                    postpStore.loadPage(1);
                                }
                            }
                        ]}
                ],
                bbar:Ext.create('Ext.PagingToolbar', {
                    store:postpStore,
                    pageSize:10,
                    displayInfo:true,
                    displayMsg:'显示 {0} - {1} 条,共计 {2} 条',
                    emptyMsg:"没有数据"
                }),
                height:400,
                width:500,
                renderTo:'showUser'
            });


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值