熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

在JavaScript中調用ASP.NET WebService的簡單方法

2022-06-13   來源: .NET編程 

  客戶端 JavaScript 調用 ASPNET WebService 的方法除了采用 WebServerhtc 和 構造 SOAPAction 的方法外下面介紹一個采用 Ajax 調用的簡單方法並且可以傳遞參數其實ASPNET WebService 就是一個網站所以Request 對象是可用的這樣傳遞參數就很容易了下面是一個WebServiceasmx的代碼

  ASMX 代碼

  <%@ WebService Language=C# CodeBehind=WebServiceasmxcs Class=WebService %>

  C# 代碼

  using System;

  using SystemCollectionsGeneric;

  using SystemLinq;

  using SystemWeb;

  using SystemWebServices;

  using SystemData;

  /// <summary>

  /// Summary description for WebService

  /// </summary>

  [WebService(Namespace = )]

  [WebServiceBinding(ConformsTo = WsiProfilesBasicProfile_)]

  [SystemComponentModelToolboxItem(false)]

  // To allow this Web Service to be called from script using ASPNET AJAX uncomment the following line

  // [SystemWebScriptServicesScriptService]

  public class WebService : SystemWebServicesWebService

  {

  [WebMethod]

  // 字符串返回測試

  public string GetServerTime()

  {

  return 當前服務器時間 + DateTimeNowToString();

  }

  [WebMethod]

  // long 類型返回測試

  public long GetServerTimeTicks()

  {

  return DateTimeNowTicks;

  }

  [WebMethod]

  // Datatable返回測試

  public DataTable GetTestDataTable()

  {

  DataTable dt = new DataTable(TestTable);

  DataRow dr;

  dtColumnsAdd(new DataColumn(id typeof(Int)));

  dtColumnsAdd(new DataColumn(text typeof(string)));

  for (int i = ; i < ; i++)

  {

  dr = dtNewRow();

  dr[] = i;

  dr[] = 列表項目 + iToString();

  dtRowsAdd(dr);

  }

  return dt;

  }

  [WebMethod]

  // List 類型測試

  public List<User> GetTestUser()

  {

  //傳遞參數傳測試

  string param = thisContextRequestQueryString[param];

  if (param == null) param= thisContextRequestForm[param];

  List<User> u_list = new List<User>();

  for (int i = ; i < ; i++)

  {

  User u = new User();

  uName = LoginName + iToString() + param = + param;

  uTitle = 孟憲會 + iToString();

  u_listAdd(u);

  }

  return u_list;

  }

  //定義一個對象 User

  public class User

  {

  public String Name { get; set; }

  public String Title { get; set; }

  }

  }

  客戶端調用的代碼

  HTML 代碼

  <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

  <html xmlns=>

  <head runat=server>

  <title>JavaScript 調用 ASPNET Web 服務測試</title>

  <script type=text/javascript>

  var xmlHttp = null;

  function createXMLHttpRequest() {

  try {

  if (windowXMLHttpRequest)

  xmlHttp = new XMLHttpRequest();

  else if (windowActiveXObject)

  xmlHttp = new ActiveXObject(MicrosoftXMLHTTP);

  }

  catch (ex) { }

  }

  function AsynRequest() {

  createXMLHttpRequest();

  if (xmlHttp == null) {

  alert(不能創建 XmlHttpRequest 對象);

  return;

  }

  xmlHttpopen(GET WebServiceasmx/GetTestUser?param=net_lover true);

  xmlHttpsetRequestHeader(Connection close);

  xmlHttponreadystatechange = function () {

  if (xmlHttpreadyState == ) {

  if (xmlHttpstatus == ) {

  var userList = xmlHttpresponseXMLgetElementsByTagName(User);

  for (i = ; i < userListlength; i++) {

  documentgetElementById(get)innerHTML += userList[i]getElementsByTagName(Name)[]firstChildnodeValue + ;

  documentgetElementById(get)innerHTML += userList[i]getElementsByTagName(Title)[]firstChildnodeValue + <br/>;

  }

  }

  }

  };

  xmlHttpsend();

  }

  function AsynPostRequest() {

  createXMLHttpRequest();

  if (xmlHttp == null) {

  alert(不能創建 XmlHttpRequest 對象);

  return;

  }

  var data = param=abc;

  xmlHttpopen(POST WebServiceasmx/GetTestUser?t= + Dateparse(new Date()) true);

  xmlHttpsetRequestHeader(Contenttype application/xwwwformurlencoded);

  xmlHttpsetRequestHeader(Contentlength datalength);

  xmlHttpsetRequestHeader(Connection close);

  xmlHttponreadystatechange = function () {

  if (xmlHttpreadyState == ) {

  if (xmlHttpstatus == ) {

  var userList = xmlHttpresponseXMLgetElementsByTagName(User);

  for (i = ; i < userListlength; i++) {

  documentgetElementById(post)innerHTML += userList[i]getElementsByTagName(Name)[]firstChildnodeValue + ;

  documentgetElementById(post)innerHTML += userList[i]getElementsByTagName(Title)[]firstChildnodeValue + <br/>;

  }

  }

  }

  };

  xmlHttpsend(data);

  }

  </script>

  </head>

  <body>

  <input type=button value=GET 方法調用 onclick=AsynRequest() />

  <input type=button value=POST方法調用 onclick=AsynPostRequest() />

  <div id=get></div>

  <div id=post></div>

  </body>

  </html>

  需要注意的是使用此方法需要在nfig裡加入以下的配置

  nfig 代碼

  <systemweb>

  <webServices>

  <protocols>

  <add name = HttpPost />

  <add name = HttpGet />

  </protocols>

  </webServices>

  </systemweb>


From:http://tw.wingwit.com/Article/program/net/201311/12604.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.