search
HomeWeb Front-endJS TutorialjQuery AJax calls asp.net webservers implementation code

aspx page code

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
 <title></title> 
  <script src="JQUERY.JS" type="text/javascript"></script> 
  <style type="text/css"><!-- 
.hover 
{ 
cursor: pointer; /*小手*/ 
background: #ffc; /*背景*/ 
} 
.button 
{ 
width: 150px; 
float: left; 
text-align: center; 
margin: 10px; 
padding: 10px; 
border: 1px solid #888; 
} 
#dictionary 
{ 
text-align: center; 
font-size: 18px; 
clear: both; 
border-top: 3px solid #888; 
} 
#loading 
{ 
border: 1px #000 solid; 
background-color: #eee; 
padding: 20px; 
margin: 100px 0 0 200px; 
position: absolute; 
display: none; 
} 
   
--></style><style type="text/css" bogus="1"><!-- 
.hover 
{ 
cursor: pointer; /*小手*/ 
background: #ffc; /*背景*/ 
} 
.button 
{ 
width: 150px; 
float: left; 
text-align: center; 
margin: 10px; 
padding: 10px; 
border: 1px solid #888; 
} 
#dictionary 
{ 
text-align: center; 
font-size: 18px; 
clear: both; 
border-top: 3px solid #888; 
} 
#loading 
{ 
border: 1px #000 solid; 
background-color: #eee; 
padding: 20px; 
margin: 100px 0 0 200px; 
position: absolute; 
display: none; 
} 
   
--></style><style type="text/css" bogus="1" bogus="1">.hover 
{ 
cursor: pointer; /*小手*/ 
background: #ffc; /*背景*/ 
} 
.button 
{ 
width: 150px; 
float: left; 
text-align: center; 
margin: 10px; 
padding: 10px; 
border: 1px solid #888; 
} 
#dictionary 
{ 
text-align: center; 
font-size: 18px; 
clear: both; 
border-top: 3px solid #888; 
} 
#loading 
{ 
border: 1px #000 solid; 
background-color: #eee; 
padding: 20px; 
margin: 100px 0 0 200px; 
position: absolute; 
display: none; 
} 
  </style> 
  <script type="text/javascript"><!-- 
    //无参数调用 
    $(document).ready(function() { 
      $(&#39;#btn1&#39;).click(function() { 
        $.ajax({ 
          type: "POST",  //访问WebService使用Post方式请求 
          contentType: "application/json", //WebService 会返回Json类型 
          url: "WebService1.asmx/HelloWorld", //调用WebService的地址和方法名称组合 ---- WsURL/方法名 
          data: "{}",     //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到    
          dataType: &#39;json&#39;, 
          success: function(result) {   //回调函数,result,返回值 
            $(&#39;#dictionary&#39;).append(result.d); 
          } 
        }); 
      }); 
    }); 
    //有参数调用 
    $(document).ready(function() { 
      $("#btn2").click(function() { 
        $.ajax({ 
          type: "POST", 
          contentType: "application/json", 
          url: "WebService1.asmx/GetWish", 
          data: "{value1:&#39;心想事成&#39;,value2:&#39;万事如意&#39;,value3:&#39;牛牛牛&#39;,value4:2009}", 
          dataType: &#39;json&#39;, 
          success: function(result) { 
            $(&#39;#dictionary&#39;).append(result.d); 
          } 
        }); 
      }); 
    }); 
     
     
    //返回集合(引用自网络,很说明问题) 
    $(document).ready(function() { 
      $("#btn3").click(function() { 
        $.ajax({ 
          type: "POST", 
          contentType: "application/json", 
          url: "WebService1.asmx/GetArray", 
          data: "{i:10}", 
          dataType: &#39;json&#39;, 
          success: function(result) { 
            $(result.d).each(function() { 
              //alert(this); 
              $(&#39;#dictionary&#39;).append(this.toString() + " "); 
              //alert(result.d.join(" | ")); 
            }); 
          } 
        }); 
      }); 
    }); 
    //返回复合类型 
    $(document).ready(function() { 
      $(&#39;#btn4&#39;).click(function() { 
        $.ajax({ 
          type: "POST", 
          contentType: "application/json", 
          url: "WebService1.asmx/GetClass", 
          data: "{}", 
          dataType: &#39;json&#39;, 
          success: function(result) { 
            $(result.d).each(function() { 
              //alert(this); 
              $(&#39;#dictionary&#39;).append(this[&#39;ID&#39;] + " " + this[&#39;Value&#39;]); 
              //alert(result.d.join(" | ")); 
            }); 
          } 
        }); 
      }); 
    }); 
    //返回DataSet(XML) 
    $(document).ready(function() { 
      $(&#39;#btn5&#39;).click(function() { 
        $.ajax({ 
          type: "POST", 
          url: "WebService1.asmx/GetDataSet", 
          data: "{}", 
          dataType: &#39;xml&#39;, //返回的类型为XML ,和前面的Json,不一样了 
          success: function(result) { 
          //演示一下捕获 
            try {  
              $(result).find("Table1").each(function() { 
                $(&#39;#dictionary&#39;).append($(this).find("ID").text() + " " + $(this).find("Value").text()); 
              }); 
            } 
            catch (e) { 
              alert(e); 
              return; 
            } 
          }, 
          error: function(result, status) { //如果没有上面的捕获出错会执行这里的回调函数 
            if (status == &#39;error&#39;) { 
              alert(status); 
            } 
          } 
        }); 
      }); 
    }); 
    //Ajax 为用户提供反馈,利用ajaxStart和ajaxStop 方法,演示ajax跟踪相关事件的回调,他们两个方法可以添加给jQuery对象在Ajax前后回调 
    //但对与Ajax的监控,本身是全局性的 
    $(document).ready(function() { 
      $(&#39;#loading&#39;).ajaxStart(function() { 
        $(this).show(); 
      }).ajaxStop(function() { 
        $(this).hide(); 
      }); 
    }); 
    // 鼠标移入移出效果,多个元素的时候,可以使用“,”隔开 
    $(document).ready(function() { 
      $(&#39;div.button&#39;).hover(function() { 
        $(this).addClass(&#39;hover&#39;); 
      }, function() { 
        $(this).removeClass(&#39;hover&#39;); 
      }); 
    }); 
     
     
   
// --></script> 
</head> 
<body> 
  <form id="form1" runat="server"> 
  <div id="switcher"> 
    <h2> 
      jQuery 的WebServices 调用</h2> 
    <div class="button" id="btn1"> 
      HelloWorld</div> 
    <div class="button" id="btn2"> 
      传入参数</div> 
    <div class="button" id="btn3"> 
      返回集合</div> 
    <div class="button" id="btn4"> 
      返回复合类型</div> 
    <div class="button" id="btn5"> 
      返回DataSet(XML)</div> 
  </div> 
  <div id="loading"> 
    服务器处理中,请稍后。 
  </div> 
  <div id="dictionary"> 
  </div> 
  </form> 
</body> 
</html>

WebService1.asmx code

using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Services; 
using System.Web.Services.Protocols; 
using System.Xml.Linq; 
using System.Collections.Generic; 
namespace jquery_Learning 
{ 
/// <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 
{ 
/// <summary> 
/// 无参数 
/// </summary> 
/// <returns></returns> 
[WebMethod] 
public string HelloWorld() 
{ 
return "Hello World "; 
} 
/// <summary> 
/// 带参数 
/// </summary> 
/// <param name="value1"></param> 
/// <param name="value2"></param> 
/// <param name="value3"></param> 
/// <param name="value4"></param> 
/// <returns></returns> 
[WebMethod] 
public string GetWish(string value1, string value2, string value3, int value4) 
{ 
return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4); 
} 
/// <summary> 
/// 返回集合 
/// </summary> 
/// <param name="i"></param> 
/// <returns></returns> 
[WebMethod] 
public List<int> GetArray(int i) 
{ 
List<int> list = new List<int>(); 
while (i >= 0) 
{ 
list.Add(i--); 
} 
return list; 
} 
/// <summary> 
/// 返回一个复合类型 
/// </summary> 
/// <returns></returns> 
[WebMethod] 
public Class1 GetClass() 
{ 
return new Class1 { ID = "1", Value = "牛年大吉" }; 
} 
/// <summary> 
/// 返回XML 
/// </summary> 
/// <returns></returns> 
[WebMethod] 
public DataSet GetDataSet() 
{ 
DataSet ds = new DataSet(); 
DataTable dt = new DataTable(); 
dt.Columns.Add("ID", Type.GetType("System.String")); 
dt.Columns.Add("Value", Type.GetType("System.String")); 
DataRow dr = dt.NewRow(); 
dr["ID"] = "1"; 
dr["Value"] = "新年快乐"; 
dt.Rows.Add(dr); 
dr = dt.NewRow(); 
dr["ID"] = "2"; 
dr["Value"] = "万事如意"; 
dt.Rows.Add(dr); 
ds.Tables.Add(dt); 
return ds; 
} 
} 
//自定义的类,只有两个属性 
public class Class1 
{ 
public string ID { get; set; } 
public string Value { get; set; } 
} 
}

For more jQuery AJax implementation code for calling asp.net webservers related articles, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.