我想從客戶端使用JavaScript訪問webservice。我已成功創建webservice。我已經成功地在網站項目中使用它,但它是Web應用程序項目。如何調用WebService客戶端?
這裏是我的HTML代碼
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
function Test() {
WebService1.HelloWorld(onS, onF);
}
function onS(r) { alert("Result"); }
function onF(er) { alert("Error"); }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference InlineScript="true" Path="~/WebService1.asmx"/>
</Services>
</asp:ScriptManager>
<input type="button" value="Call Service" onclick="Test()" />
</div>
</form>
</body>
</html>
這裏是WebService的代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace TestWebService
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
下面這段代碼是行不通的。
我使用的是框架3.5和VS2012。