Ext.data.Connection應該說提供了最為簡潔的與背景非同步呼叫功能
執行個體如下
首先是aspx頁面內容(省略了head和對js檔案引用的部分)
view plaincopy to clipboardprint?
<body>
<div id="form1"></div>
</body>
<body>
<div id="form1"></div>
</body>
關於Extjs的內容寫在jsdataConnection.js檔案中
內容如下:
view plaincopy to clipboardprint?
Ext.onReady(function() {
var form1 = new Ext.form.FormPanel({
width: 350,
frame: true,
renderTo: "form1",
labelWidth: 80,
title: "form1",
bodyStyle: "padding:5px 5px 0",
defaults: { width: 150, xtype: "textfield" },
items: [
{
fieldLabel: "ID",
id: "ID",
anchor: "90%"
},
{
fieldLabel: "name",
id: "name",
anchor: "90%"
}
],
buttons: [
{ text: "確定", handler: function() { doUpdate(); } }
]
});
//建立串連
var conn = new Ext.data.Connection({
autoAbort: false,
defaultHeaders: {
referer: 'http://localhost:1068'
//此處defaultHeaders可以不設定
},
disableCaching: false,
extraParams: {
params: 'Update'
},
method: 'post',
timeout: 300,
url: 'Handler.ashx'//此處如不指定,則預設訪問當前頁面
});
/*
其中
①autoAbort:該request是否會自動斷開(預設值false)。
②disableCaching:設定為true就會添加一個獨一無二的cache-buster參數來擷取請求(預設值為true)。
③extraParams:這些屬性在該Connection發起的每次請求中作為外部參數使用
④timeout:串連的逾時時間
⑤method:請求時使用的預設的http方法。【post】【get】
⑥url:請求的網頁地址,關於url最好使用ashx分頁檔,
如果用aspx的話要把所有的頁面元素都清除乾淨,否則前台接收到的內容會有問題。
*/
function doUpdate() {
//在建立了conn之後,可以調用request()函數發送請求,處理返回的結果,如下面的代碼所示。
conn.request({
success: function(response) {
Ext.Msg.alert('info', response.responseText);
//response.responseText為返回的資訊
},
failure: function() {
Ext.Msg.alert('warn', 'failure');
}
});
}
//success:成功後執行的方法(參數返回response)
//failure:失敗時執行的方法
});
Ext.onReady(function() {
var form1 = new Ext.form.FormPanel({
width: 350,
frame: true,
renderTo: "form1",
labelWidth: 80,
title: "form1",
bodyStyle: "padding:5px 5px 0",
defaults: { width: 150, xtype: "textfield" },
items: [
{
fieldLabel: "ID",
id: "ID",
anchor: "90%"
},
{
fieldLabel: "name",
id: "name",
anchor: "90%"
}
],
buttons: [
{ text: "確定", handler: function() { doUpdate(); } }
]
});
//建立串連
var conn = new Ext.data.Connection({
autoAbort: false,
defaultHeaders: {
referer: 'http://localhost:1068'
//此處defaultHeaders可以不設定
},
disableCaching: false,
extraParams: {
params: 'Update'
},
method: 'post',
timeout: 300,
url: 'Handler.ashx'//此處如不指定,則預設訪問當前頁面
});
/*
其中
①autoAbort:該request是否會自動斷開(預設值false)。
②disableCaching:設定為true就會添加一個獨一無二的cache-buster參數來擷取請求(預設值為true)。
③extraParams:這些屬性在該Connection發起的每次請求中作為外部參數使用
④timeout:串連的逾時時間
⑤method:請求時使用的預設的http方法。【post】【get】
⑥url:請求的網頁地址,關於url最好使用ashx分頁檔,
如果用aspx的話要把所有的頁面元素都清除乾淨,否則前台接收到的內容會有問題。
*/
function doUpdate() {
//在建立了conn之後,可以調用request()函數發送請求,處理返回的結果,如下面的代碼所示。
conn.request({
success: function(response) {
Ext.Msg.alert('info', response.responseText);
//response.responseText為返回的資訊
},
failure: function() {
Ext.Msg.alert('warn', 'failure');
}
});
}
//success:成功後執行的方法(參數返回response)
//failure:失敗時執行的方法
});
其中Handler.ashx的內容如下
(建立ashx檔案,我對內容沒有做任何改動,除了加上一句注釋之外)
view plaincopy to clipboardprint?
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//返回字串Hello World
context.Response.Write("Hello World");
}
public bool IsReusable {
get {
return false;
}
}
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/gishero/archive/2010/01/05/5133922.aspx