VS2008中使用JavaScript調(diào)用WebServices
最近這幾天任務(wù)完成了,也沒什么重要的事情,抽空學(xué)習(xí)了一下WebServices的知識,感覺還是挺有意思,難度也不是很大。
首先,用VS2008創(chuàng)建一個asp.net網(wǎng)站
其次,項目 右鍵—>添加新項—>Web 服務(wù) 如下圖:

就會產(chǎn)生WebService.cs和WebService.asmx兩個文件
在WebService.cs中添加代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
/// <summary>
///WebService 的摘要說明
/// </summary>
[WebService(Namespace = " [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//注意添加下面代碼//
[ScriptService]
//若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請取消對下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用設(shè)計的組件,請取消注釋以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public int GetSum(int a, int b)
{
int sum = a + b;
return sum;
}
}
Default.aspx頁面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " <html xmlns=" <head runat="server">
<title></title>
</head>
<script language="javascript">
function Method(obj)
{
document.getElementById("txtSum").value = obj;
}
function Hello()
{
WebService.HelloWorld(backMethod);
}
function getSum()
{
var a,b;
a = document.getElementById("txtA").value;
b = document.getElementById("txtB").value;
try
{
WebService.GetSum(a, b, Method);
}
catch(err)
{
alert(err.description);
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference InlineScript="True" Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
<input type="button" id="btHello" value="Hello" onclick="Hello();" /><br />
<input type="text" id="txtA" value="" />+
<input type="text" id="txtB" value="" />=
<input type="text" id="txtSum" value="" />
<input type="button" id="btSum" value="求和" onclick="getSum();" /><br />
</div>
</form>
</body>
</html>
通過以上方法就可以輕松的調(diào)用WebService中的方法,WebService中也可以返回一個DataSet結(jié)果集。
后面還得繼續(xù)學(xué)習(xí)WebService的知識。
如果大家有好的WebService學(xué)習(xí)的資料或者是網(wǎng)站的話,拿出來分享一下,以方便大家共同學(xué)習(xí)、交流。
- vs2012創(chuàng)建的ado.net模型無法實例化的解決方案
- vs2012 error c4996: This function or variable may be unsafe
- VS2010發(fā)布Web網(wǎng)站技術(shù)攻略
- 詳解VS2012發(fā)布網(wǎng)站步驟
- VS2010新建站點發(fā)布并訪問步驟詳解
- vs2010制作簡單的asp.net網(wǎng)站
- VS中C#讀取app.config數(shù)據(jù)庫配置字符串的三種方法
- VS2010制作第一個簡單網(wǎng)站
- C# WCF簡單入門圖文教程(VS2010版)
- Visual Studio 2013更新內(nèi)容簡介
相關(guān)文章
JavaScript實現(xiàn)替換字符串中最后一個字符的方法
這篇文章主要介紹了JavaScript實現(xiàn)替換字符串中最后一個字符的方法,涉及javascript字符串的轉(zhuǎn)換與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
javascript結(jié)合html5 canvas實現(xiàn)(可調(diào)畫筆顏色/粗細(xì)/橡皮)的涂鴉板
js+html5 canvas實現(xiàn)的涂鴉畫板特效,可調(diào)畫筆顏色|粗細(xì)|橡皮,可以保存涂鴉效果為圖片編碼,測試了下還不錯,感興趣的朋友可以參考下2013-04-04
JavaScript中window和document用法詳解
這篇文章主要介紹了JavaScript中window和document用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Three.js利用orbit controls插件(軌道控制)控制模型交互動作詳解
這篇文章主要給大家介紹了關(guān)于Three.js利用orbit controls插件,也就是軌道控制來控制模型交互動作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09

