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)文章
解決bootstrap中下拉菜單點擊后不關(guān)閉的問題
今天小編就為大家分享一篇解決bootstrap中下拉菜單點擊后不關(guān)閉的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08JS數(shù)組合并push與concat區(qū)別分析
這篇文章主要介紹了JS數(shù)組合并push與concat區(qū)別,結(jié)合實例形式分析了JavaScript中針對數(shù)組合并操作使用push與concat的區(qū)別,需要的朋友可以參考下2015-12-12