且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Ext直接调用ASP.Net Ajax Webservice

更新时间:2022-09-16 10:26:44

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

 Ext直接调用ASP.Net Ajax Webservice

Ext直接调用ASP.Net Ajax Webservice

张志敏所有文章遵循创作共用版权协议,要求署名、非商业 、保持一致。在满足创作共用版权协议的基础上可以转载,但请以超链接形式注明出处。

本博客已经迁移到 GitHub , 围观地址: http://beginor.github.io/

本文转自张志敏博客园博客,原文链接:http://www.cnblogs.com/beginor/archive/2008/04/26/1172189.html,如需转载请自行联系原作者