.net WebService学习(二)

 

 

.net调用Webwebservice

通常是把WebServer发布到iis,然后在另一个程序中调(这里为了方便直接在本程序中调用演示)

 1.项目中的引用选择添加服务引用,地址输入刚才那个页面的地址。

然后看项目Service References文件夹

2.新建一个WebServerTest.aspx页面,在.cs中写

protected void Page_Load(object sender, EventArgs e)
{
ServiceReference1.TestServerSoapClient testServer = new ServiceReference1.TestServerSoapClient();
string str1= testServer.Compute(1,2);

Response.Write(str1 + "," + str2);
}

有结果输出刚调用成功了。

三、前端JS调用Webwebservice

1.把TestServer.asmx 文件的允许ajax调用web服务下面一行代码取消注释

 2.添加一个WebServerData.html页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src=" http://libs.baidu.com/jquery/1.11.1/jquery.min.js "></script>
<script type="text/javascript">
$(function () {
$("#getdata").click(function () {
$.ajax({
type: 'POST',
url: 'TestServer.asmx/Compute',
data: '{ a:1,b:2}',
dataType: 'json',
contentType: "application/json",
success: function (data) {
$("#data").append(data.d);
}
});
});
});
</script>
</head>
<body>
<a id="getdata" href="javascript:void(0);">获取webservice数据</a>
<div id="data"></div>
</body>
</html>

点击a显示结果则成功。

猜你喜欢

转载自www.cnblogs.com/nayilvyangguang/p/10080423.html