javascript 如何调用WebService
服务器端代码:
using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService ()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string user)
{
return "<p style='backGround-color:red;'>"+user+ " Hello World!</p>";
}
}
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService ()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string user)
{
return "<p style='backGround-color:red;'>"+user+ " Hello World!</p>";
}
}
客户端代码:
<script type="text/javascript">
//下面的脚本调用WebService取回数据显示在webserviceDataDiv
function getDataFromWebService()
{
WebService.HelloWorld($get("Text1").value,OnGetDataFromWebServiceCompleted,null,null);
}
function OnGetDataFromWebServiceCompleted(data)
{
$get("webserviceDataDiv").innerHTML=data;
}
</script>
<script type="text/javascript">
function getDataByServerMethod()
{
PageMethods.ShowDetailData($get('Text1').value,OnGetDataFromWebMethodCompleted);
}
function OnGetDataFromWebMethodCompleted(data)
{
$get("webserviceDataDiv").innerHTML=data;
}
</script>
<input id="Text1" type="text" value="me_sa" />
<input id="Button3" type="button" value="GetDataFromWebService" onclick="getDataFromWebService();" />
<div id="webserviceDataDiv">
</div>
//下面的脚本调用WebService取回数据显示在webserviceDataDiv
function getDataFromWebService()
{
WebService.HelloWorld($get("Text1").value,OnGetDataFromWebServiceCompleted,null,null);
}
function OnGetDataFromWebServiceCompleted(data)
{
$get("webserviceDataDiv").innerHTML=data;
}
</script>
<script type="text/javascript">
function getDataByServerMethod()
{
PageMethods.ShowDetailData($get('Text1').value,OnGetDataFromWebMethodCompleted);
}
function OnGetDataFromWebMethodCompleted(data)
{
$get("webserviceDataDiv").innerHTML=data;
}
</script>
<input id="Text1" type="text" value="me_sa" />
<input id="Button3" type="button" value="GetDataFromWebService" onclick="getDataFromWebService();" />
<div id="webserviceDataDiv">
</div>
如果你使用VS2008你可以得到这样的提示:
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |