ExtJS发送POST请求 参数格式为JSON

背景

    这要从我比较懒说起。技术框架ExtJS + resteasy,默认请求方式是ajax get,这后台方法就要写很多@QueryParam来获取参数。我比较喜欢前台用ajax post请求,后台方法参数就是一个map,所有前台参数映射成map的key-value,然后将map --> json(com.alibaba.fastjson) --> pojo对象

    这里不得不赞一下fastjson转化数据类型很智能,诸如integer、date类型基本不需要自定义方法就完美转换。

例子

    通过google找到一种很方便的解决方案,自定义用代理proxy来实现发送POST请求,并指定参数类型为json。

Ext.define('Ext.ux.data.proxy.JsonAjaxProxy', {
extend:'Ext.data.proxy.Ajax',
alias:'proxy.jsonajax',
actionMethods : {
create: "POST",
read: "POST",
update: "POST",
destroy: "POST"
},
buildRequest:function (operation) {
var request = this.callParent(arguments);
       // For documentation on jsonData see Ext.Ajax.request
        request.jsonData = request.params;
        request.params = {};
       return request;
},
/*
* @override
* Inherit docs. We don't apply any encoding here because
* all of the direct requests go out as jsonData
*/
applyEncoding: function(value){
return value;
}
});

 

 

使用也很方便,将proxy的type设置为jsonajax即可。

proxy : {
   type : 'jsonajax'
   ...
}

 

posted @ 2015-12-17 13:43  飞起航  阅读(7352)  评论(0编辑  收藏  举报