Ext直接調用ASP.Net Ajax Webservice
ExtJS發布了2.1版,經過測試,這個版本可以直接調用ASP.Net Ajax的WebService,示例代碼如下:
1. WebService代碼:
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class TestWebService : System.Web.Services.WebService { [WebMethod] public string HelloWorld(string firstName, string lastName) { return string.Format("Hello {0} {1}", firstName, lastName); } [WebMethod] public string ExceptionMethod(int param) { throw new NotImplementedException("該方法未實現"); } }
2. 客戶端調用代碼:
function CallHelloWorld() { Ext.Ajax.request({ url: 'TestWebService.asmx/HelloWorld', // Webservice的地址以及方法名 jsonData: { firstName: 'AAA', lastName: 'BBB' }, // json 形式的參數 method: 'POST', // poste 方式傳遞 success: onSuccess, failure: onFailure }); } function CallExceptionMethod() { Ext.Ajax.request({ url: 'TestWebService.asmx/ExceptionMethod', jsonData: { param: 3 }, method: 'POST', success: onSuccess, failure: onFailure }); } function onSuccess(request, options) { // 服務器返回json形式的結果 var result = Ext.util.JSON.decode(request.responseText); Ext.Msg.alert('è?”?????“???', result.d); } function onFailure(request, options) { alert(request.responseText); }
3. 返回結果分別為:

張志敏所有文章遵循創作共用版權協議,要求署名、非商業 、保持一致。在滿足創作共用版權協議的基礎上可以轉載,但請以超鏈接形式注明出處。
本博客已經遷移到 GitHub , 圍觀地址: https://beginor.github.io/
浙公網安備 33010602011771號