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. 返回结果分别为:

 image

image

posted @ 2008-04-26 15:13  张志敏  阅读(2855)  评论(3编辑  收藏  举报