2015-01-16 188 views
1

加載商店,我需要通過跨域POST請求如何通過POST請求

Ext.define('MyDesktop.desktop.store.DesktopShortcutStore', { 


    extend: 'Ext.data.Store', 


    requires: [ 
    'MyDesktop.desktop.model.DesktopShortcutModel' 
    ], 


    constructor: function(cfg) { 
    var me = this; 
    cfg = cfg || {}; 
    me.callParent([Ext.apply({ 
     storeId: 'DesktopShortcutStore', 
     model: 'MyDesktop.desktop.model.DesktopShortcutModel', 
     autoLoad: false, 
     proxy: { 
     type: 'ajax', 
     url: getApiUrl() + 'account/desktop-shortcuts-list', 
     cors: true, 
     reader: { 
      type: 'json', 
      rootProperty: 'items', 
      successProperty: 'success' 
     }, 
     paramsAsJson: true, 
     actionMethods: { 
      read: 'POST' 
     }, 
     extraParams: { 
      fff: 'zzz' 
     } 
     } 
    }, cfg)]); 
    } 


}); 

加載店,但我有選擇,沒有「FFF」參數要求。

通過jQuery跨域正常工作。

使用

Ext.Ajax.useDefaultXhrHeader = false; 
Ext.Ajax.cors = true; 

是不是有幫助。

我在文檔中找不到任何信息。

+0

通常,飛行前請求(OPTIONS)不需要額外的標頭,因爲它只是檢查服務器是否允許來源。當飛行前結果是「合法的」時,實際的POST如下所示。 –

+0

通過POST加載商店的快捷方式是使用Ext.Ajax.request並自己分析響應或使用JSON響應屬性。然後遍歷記錄並加載到商店中。 –

+0

我相信你不能對另一個域@BenjaminE做Ajax請求。 (由於http://en.wikipedia.org/wiki/Same-origin_policy)這就是他使用CORS的原因。另一個選擇是使用JSONP代理,indapublic(儘管應該可以使用CORS) –

回答

0

這應該是最簡單的解決方案。直接通過代理和POST加載商店可能需要一些覆蓋,不確定是否值得花費(需要更多調查)。

var store = ...; 

Ext.Ajax.request({ 
    method: 'POST', 
    url: '...', 
    withCredentials: true, // for identity cookies 
    success: function(response) { 
     // given json array is returned 
     var data = Ext.decode(response.responseText); 

     Ext.each(data, function(r) { 
      store.add(r); 
     }); 
    } 
});