VS2008中使用JavaScript調(diào)用WebServices
最近這幾天任務(wù)完成了,也沒(méi)什么重要的事情,抽空學(xué)習(xí)了一下WebServices的知識(shí),感覺(jué)還是挺有意思,難度也不是很大。
首先,用VS2008創(chuàng)建一個(gè)asp.net網(wǎng)站
其次,項(xiàng)目 右鍵—>添加新項(xiàng)—>Web 服務(wù) 如下圖:
就會(huì)產(chǎn)生WebService.cs和WebService.asmx兩個(gè)文件
在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 的摘要說(shuō)明
/// </summary>
[WebService(Namespace = " [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//注意添加下面代碼//
[ScriptService]
//若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public int GetSum(int a, int b)
{
int sum = a + b;
return sum;
}
}
Default.aspx頁(yè)面
<%@ 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>
通過(guò)以上方法就可以輕松的調(diào)用WebService中的方法,WebService中也可以返回一個(gè)DataSet結(jié)果集。
后面還得繼續(xù)學(xué)習(xí)WebService的知識(shí)。
如果大家有好的WebService學(xué)習(xí)的資料或者是網(wǎng)站的話,拿出來(lái)分享一下,以方便大家共同學(xué)習(xí)、交流。
- vs2012創(chuàng)建的ado.net模型無(wú)法實(shí)例化的解決方案
- vs2012 error c4996: This function or variable may be unsafe
- VS2010發(fā)布Web網(wǎng)站技術(shù)攻略
- 詳解VS2012發(fā)布網(wǎng)站步驟
- VS2010新建站點(diǎn)發(fā)布并訪問(wèn)步驟詳解
- vs2010制作簡(jiǎn)單的asp.net網(wǎng)站
- VS中C#讀取app.config數(shù)據(jù)庫(kù)配置字符串的三種方法
- VS2010制作第一個(gè)簡(jiǎn)單網(wǎng)站
- C# WCF簡(jiǎn)單入門圖文教程(VS2010版)
- Visual Studio 2013更新內(nèi)容簡(jiǎn)介
相關(guān)文章
JS實(shí)現(xiàn)的郵箱提示補(bǔ)全效果示例
這篇文章主要介紹了JS實(shí)現(xiàn)的郵箱提示補(bǔ)全效果,涉及javascript正則匹配、事件響應(yīng)及頁(yè)面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2018-01-01解決bootstrap中下拉菜單點(diǎn)擊后不關(guān)閉的問(wèn)題
今天小編就為大家分享一篇解決bootstrap中下拉菜單點(diǎn)擊后不關(guān)閉的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08JS數(shù)組合并push與concat區(qū)別分析
這篇文章主要介紹了JS數(shù)組合并push與concat區(qū)別,結(jié)合實(shí)例形式分析了JavaScript中針對(duì)數(shù)組合并操作使用push與concat的區(qū)別,需要的朋友可以參考下2015-12-12