ExtJS4.2:AJAX提交資料的三種方式,80%人都沒用過第三種

來源:互聯網
上載者:User
文章目錄
  • 範例程式碼
  •  執行結果
  • 伺服器端代碼
  • 範例程式碼
  • 執行結果
  • 伺服器端代碼
  • 範例程式碼
  •  執行結果
  • 伺服器端代碼
  • 配置為第一種方式的代碼
  • 配置為第二種方式的代碼
  • 配置為第三種方式的代碼
參考資料

HTTP:https://zh.wikipedia.org/wiki/HTTP

MIME:https://zh.wikipedia.org/wiki/MIME

國內博文:http://blog.csdn.net/gueter/article/details/1524447

第一種方式範例程式碼
 1 var data = { 2     name: '段光偉', 3     email: 'shijiucha@qq.com' 4 }; 5  6 Ext.Ajax.request({ 7     url: '../handlers/GetData.ashx', 8     method: 'GET', 9     params: { users: Ext.encode(data) }10 });
 執行結果

伺服器端代碼
1 context.Response.Write(context.Request.QueryString["users"]);
第二種方式範例程式碼
 1 var data = { 2     name: '段光偉', 3     email: 'shijiucha@qq.com' 4 }; 5  6 Ext.Ajax.request({ 7     url: '../handlers/GetData.ashx', 8     method: 'POST', 9     params: { users: Ext.encode(data) }10 });
執行結果

伺服器端代碼
1 context.Response.Write(context.Request.Form["users"]);
第三種方式範例程式碼
 1 var data = { 2     name: '段光偉', 3     email: 'shijiucha@qq.com' 4 }; 5  6 Ext.Ajax.request({ 7     url: '../handlers/GetData.ashx', 8     method: 'POST', 9     jsonData: data10 });
 執行結果

伺服器端代碼
1 StreamReader sr = new StreamReader(context.Request.InputStream);2 context.Response.Write(sr.ReadToEnd());
ExtJs中Proxy的配置配置為第一種方式的代碼
 1 Ext.define('Demo.model.User', { 2     extend: 'Ext.data.Model', 3     fields: [ 4         { name: 'name' }, 5         { name: 'email' } 6     ], 7     proxy: { 8         type: 'ajax', 9         api: {10             create: '../handlers/GetData.ashx'11         },12         actionMethods: {13             create : 'GET',14             read   : 'GET',15             update : 'GET',16             destroy: 'GET'17         },18         reader: {19             type: 'json',20             root: 'users'21         },22         writer: {23             type: 'json',24             encode: true,25             root: 'users'26         }27     }28 });
配置為第二種方式的代碼
 1 Ext.define('Demo.model.User', { 2     extend: 'Ext.data.Model', 3     fields: [ 4         { name: 'name' }, 5         { name: 'email' } 6     ], 7     proxy: { 8         type: 'ajax', 9         api: {10             create: '../handlers/GetData.ashx'11         },12         reader: {13             type: 'json',14             root: 'users'15         },16         writer: {17             type: 'json',18             encode: true,19             root: 'users'20         }21     }22 });
配置為第三種方式的代碼
 1 Ext.define('Demo.model.User', { 2     extend: 'Ext.data.Model', 3     fields: [ 4         { name: 'name' }, 5         { name: 'email' } 6     ], 7     proxy: { 8         type: 'ajax', 9         api: {10             create: '../handlers/GetData.ashx'11         },12         reader: {13             type: 'json',14             root: 'users'15         }16     }17 });
備忘

這裡沒有總結著三種方式的使用情境,請大家給我個建議吧。我大概的想法是這樣的。

  • 擷取資料:如果參數少用第一種,否則就用第二種。多少看瀏覽器的限制。
  • 發送資料:如果資料少就用第二種,否則就用第三種。多少看瀏覽器的限制。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.