<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %> <script runat="server" language="C#"> public string JsPath { get { return this.ClientScript.GetWebResourceUrl(typeof(ScriptManager), "MicrosoftMvcAjax.debug.js"); } } </script> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>TestWebServiceInvoke</title> <script src="http://www.cnblogs.com/Scripts/MicrosoftAjax.js" type="text/javascript"></script> <%-- <script src="http://www.cnblogs.com/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>--%> <%--<script type="text/javascript" src="<%= JsPath %>"></script>--%> <script src="/Services/WebService1.asmx/js" type="text/javascript"></script> <script type="text/javascript"> function getInfo() { MvcApp1.Services.WebService1.RenderUserInfo("吴x", "123password", success); } function success(result) { // var res = String.Format("Name={0},Pwd={1},Birthday={2}", result.Name, result.Password, result.Birthday); // alert(res); alert(result.Name); } </script> </head> <body> <div> <input type="button" value="getInfo" onclick="getInfo()" /> </div> </body> </html> ////////////////////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using MvcApp1.Models; namespace MvcApp1.Services { /// <summary> /// WebService1 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public User RenderUserInfo(string name,string password) { User u=new User(); u.Name=name; u.Password=password; u.Birthday=DateTime.Now; return u; } } }