首先构建 asp.NET web应用程序,
增加web服务 服务名字叫 WebService1
namespace ASPNET
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对本行的注释。
public class WebService1 : System.Web.Services.WebService
{
[WebMethod(Description="世界你好")]
public string HelloWorld(string s)
{
return "你好"+s+"\n今年情人节怎么过啊";
}
}
}
前段的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
http://serverapi.arcgisonline.com/jsapi/arcgis/2.6/js/dojo/dojo/dojo.js
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btnHelloWorld").click(function () {
name= $("#name").val().toString()
var fname = $("#name").val().toString();//获取文本框的内容
$.ajax({
type: "post",
contentType: "application/json;utf-8;", //输出格式为json
url: "WebService1.asmx/HelloWorld", //调服务的地址
data: '{"s":"' + name + '"}', //传递的参数,够早Json格式的字符串,ajax中的data:"{}"是用于传递方法中的参数,格式为:data:"{paraName:paraValue}",如果该方法无参数,则格式为:data:"{}"
success: function (data, textStatus) {
alert(data.d);
}
});
});
});
</script>
</head>
<body>
<p>
输入你的姓名<input id="name" type="text" /><input id="btnHelloWorld" type="button"
value="button" /></p>
</body>
</html>
使用dojo访问
<script type="text/javascript">
dojo.addOnLoad(function () {
var btn = dojo.byId("btnHelloWorld");
dojo.connect(btn, "onclick", function () {
var name = "张三";
var xhrArgs = {
url: "WebService1.asmx/HelloWorld",
handleAs: "json",
headers: { "Content-Type": "application/json" },
postData: '{"s":"' + name + '"}',
load: function (data) {
alert(data.d);
},
error: function (error) {
alert("fsdfsd");
}
};
dojo.xhrPost(xhrArgs);
});
});
</script>
本文介绍了一个ASP.NET Web Service的创建过程及其前后端调用示例,前端通过jQuery和Dojo两种方式实现了对Web Service的调用。

617

被折叠的 条评论
为什么被折叠?



