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

来源:互联网 发布:淘宝包包代销货源 编辑:程序博客网 时间:2024/05/17 02:45

背景

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

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

例子

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

  1. Ext.define('Ext.ux.data.proxy.JsonAjaxProxy', {
  2. extend:'Ext.data.proxy.Ajax',
  3. alias:'proxy.jsonajax',
  4. actionMethods : {
  5. create: "POST",
  6. read: "POST",
  7. update: "POST",
  8. destroy: "POST"
  9. },
  10. buildRequest:function (operation) {
  11. var request = this.callParent(arguments);
  12.        // For documentation on jsonData see Ext.Ajax.request
  13.        request.jsonData = request.params;
  14.        request.params = {};
  15.        return request;
  16. },
  17. /*
  18. * @override
  19. * Inherit docs. We don't apply any encoding here because
  20. * all of the direct requests go out as jsonData
  21. */
  22. applyEncoding: function(value){
  23. return value;
  24. }
  25. });


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

  1. proxy : {
  2.    type : 'jsonajax'
  3.    ...
  4. }

0 0
原创粉丝点击