2012-03-03 102 views
1

我试图让EXT JSON存储使用JSON发送数据,但它似乎没有工作。 下面是简单的代码:Ext.data.store JSON格式的POST数据问题

 var myStore = new Ext.data.Store({ 
    //model: 'User', 
    proxy: { 
     type: 'ajax', 
     url: '/users.svc', 
     reader: { 
      type: 'json', 
      root: 'users' 
     }, 
     writer: { 
      type: 'json', 
      root: 'data' 
     }, 
     actionMethods: { 
      create: 'POST', read: 'POST', update: 'POST', destroy: 'POST' 
     }, 
     extraParams: { test: 'test' } 
    }, 
     listeners: { 
      beforeload: function (store, operation, options) { 
       //alert(operation.params); 
      } 
     }, 
    autoLoad: true 
}); 

既然我定义JSON“作家”,我期望parameterswould使用JSON被发送到服务器。 但是它仍然定期做POST具有以下机身: test=test&page=1&start=0&limit=25

虽然我的期望是,POST应具备以下机构:{test:'test',page:1,start:0}

我希望得到任何帮助

附:我正在使用EXTJS 4.0.7

回答

0

proxy的定义更改为model

E.g.

Ext.define('User', { 
extend: 'Ext.data.Model', 
fields: ['id', 'name', 'email'], 
proxy: { 
    type: 'ajax', 
    url: '/users.svc', 
    reader: { 
     type: 'json', 
     root: 'users' 
    }, 
    writer: { 
     type: 'json', 
     root: 'data' 
    }, 
    actionMethods: { 
     create: 'POST', read: 'POST', update: 'POST', destroy: 'POST' 
    }, 
    extraParams: { test: 'test' } 
} 
}); 

然后配置店里像这样:

var myStore = new Ext.data.Store({ 
    model: 'User' 
    }); 

商店将使用在模型中指定的代理。 希望这有助于!

2

proxy.read总是使用参数,可以不jsonData,所以store.load不能发布JSON

http://ahlearns.wordpress.com/2012/08/16/ext-js-4-load-a-data-store-using-json-params/

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

}); 

希望这有助于!

+0

如何在请求正文中发送?我看到了链接,但无法弄清楚这一点? – Isaac 2013-05-15 05:58:45

+0

这个问题是关于“JSON格式的Ext.data.store POST数据”, 所以使用 store.load({params:{a:1,b:2}}) 将参数传递为json – Song 2013-06-05 14:40:53

+0

看起来像这样应该工作,不是吗?但是,如何区分这些操作,而不需要像' 'api:{create:'server/jsonProxy.php?action = create',读取:undefined,update:'server/jsonProxy.php?action =更新',销毁:'server/jsonProxy.php?action = destroy'},' – 2013-11-17 15:03:01