When using jQuery to implement Ajax in Asp.NET, aspx, ashx, and WebService can be used on the server side. I recently studied the WebService method. This post gives a very detailed example of jQuery Ajax method call jQuery Ajax method call Asp.Net WebService detailed example code, but does not give a detailed explanation. There were several details that I didn't pay attention to at first, but it took a lot of effort to solve them:
1) The key field name in the client data must be strictly consistent with the server-side method parameters.
For example, the client:
//有参数调用 $(document).ready(function () { $("#btn2").click(function () { $.ajax({ type: "POST", contentType: "application/json", url: "WebService.asmx/GetWish", data: "{value1:'心想事成',value2:'万事如意',value3:'牛牛牛',value4:2009}", dataType: 'json', success: function (result) { $('#dictionary').append(result.d); } }); }); });
The GetWish function parameters on the server side must be written as value1, value2, value3, value4:
[WebMethod] public string GetWish(string value1, string value2, string value3, int value4) { return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4); }
2) The method for the client to pass objects to the server:
Client Code:
//传入对象 $(function () { $("#btn6").click(function () { obj = new Object(); obj.ID = "1"; obj.Value = "aaa"; //'{"obj":{"ID":"1",Value:"Horse"}}' var d = '{"obj":' + JSON.stringify(obj) + '}'; $.ajax({ type: "POST", //访问WebService使用Post方式请求 contentType: "application/json", //WebService 会返回Json类型 url: "WebService.asmx/ParmsObject", //调用WebService的地址和方法名称组合 ---- WsURL/方法名 data: d, //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到 dataType: 'json', success: function (result) { alert(result.d); }, error: function (result) { alert("fail"); } }); }); });
Server-side code:
[WebMethod] public string ParmsObject(Class1 obj) { return obj.ID + ":" + obj.Value; }
Debugging environment: VS2010+jquery-1.3.2.min.js

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor