前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-3.2-vsdoc2.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" >
jQuery(function () {
jQuery.ajax({
type: "Post",
url: "/WebService1.asmx/HelloWorld",
data:"{name:'likong'}",
dataType: "json",
contentType:"application/json;charset=utf-8",
success: function (result) {
window.alert(result.d);
},
error:function(err){
window.alert(err.status); } }) })
</script>
</head>
</html>

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization; namespace WebApplication1
{
/// <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(string name)
{
return "na:" + name + "";
}
}
}

Ajax调用WebService返回List数组:

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-3.2-vsdoc2.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" >
jQuery(function () {
jQuery.ajax({
type: "Post",
url: "/WebService1.asmx/HelloWorld",
data:"{name:'likong',sex:'男',age:'22'}",
dataType: "json",
contentType:"application/json;charset=utf-8",
success: function (result) {
var str;
jQuery.each(result.d, function (index,values) {
window.alert("第" + (index+) + "项;值为" + values + ""); }) },
error:function(err){
window.alert(err.status); } }) })
</script>
</head>
</html>

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization; namespace WebApplication1
{
/// <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 List<string> HelloWorld(string name,string sex,string age)
{
List<string> list = new List<string>();
list.Add(name);
list.Add(sex);
list.Add(age);
return list; }
}
}

Ajax调用WebService返回自定义类:

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-3.2-vsdoc2.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" >
jQuery(function () {
jQuery.ajax({
type: "Post",
url: "/WebService1.asmx/HelloWorld",
data:"{name:'likong',sex:'男',age:'22'}",
dataType: "json",
contentType:"application/json;charset=utf-8",
success: function (result) { window.alert(result.d.name + " " + result.d.sex + " " + result.d.age); },
error:function(err){
window.alert(err.status); } }) })
</script>
</head>
</html>

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization; namespace WebApplication1
{
/// <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 PerSon HelloWorld(string name,string sex,string age)
{
PerSon per = new PerSon();
per.name = name;
per.sex = sex;
per.age = age;
return per; }
} public class PerSon
{
public string name {get;set; }
public string sex { get; set; }
public string age { get; set; }
}
}

Ajax调用WebService返回自定义类集合:

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-3.2-vsdoc2.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" >
jQuery(function () {
jQuery.ajax({
type: "Post",
url: "/WebService1.asmx/HelloWorld",
data:"{name:'likong',sex:'男',age:'22'}",
dataType: "json",
contentType:"application/json;charset=utf-8",
success: function (result) {
jQuery.each(result.d, function (index, values) { window.alert("第" + (index + 1) + "项:" + values.name + " " + values.sex + " " + values.age); }) },
error:function(err){
window.alert(err.status); } }) })
</script>
</head>
</html>

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization; namespace WebApplication1
{
/// <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 List<PerSon> HelloWorld(string name, string sex, string age)
{
return new List<PerSon>() {
new PerSon(){
name=name,
sex=sex,
age=age
},
new PerSon(){
name="张三",
sex="女",
age=""
} };
}
} public class PerSon
{
public string name {get;set; }
public string sex { get; set; }
public string age { get; set; }
}
}

Ajax调用WebService返回Dictionary<string,string>集合:

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-3.2-vsdoc2.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" >
jQuery(function () {
jQuery.ajax({
type: "Post",
url: "/WebService1.asmx/HelloWorld",
data:"{name:'likong',sex:'男',age:'22'}",
dataType: "json",
contentType:"application/json;charset=utf-8",
success: function (result) {
jQuery.each(result.d, function (key, values) { window.alert("" + key+ " :" + values); }) },
error:function(err){
window.alert(err.status); } }) })
</script>
</head>
</html>

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization; namespace WebApplication1
{
/// <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 Dictionary<string,string> HelloWorld(string name, string sex, string age)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("",name);
dic.Add("",sex);
dic.Add("", age); return dic;
}
}
}

Ajax调用WebService返回dataset:

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-3.2-vsdoc2.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" >
jQuery(function () {
jQuery.ajax({
type: "post",
url: "/WebService1.asmx/HelloWorld",
data:"name=likong&sex=男&age=22",
dataType: "xml",
success: function (result) {
jQuery.each(jQuery.find("Table1", result), function () { window.alert(jQuery(this).find("name").text() + " " + jQuery(this).find("sex").text() + " " + jQuery(this).find("age").text()); }) },
error: function (data) {
//200的响应也有可能被认定为error,responseText中没有Message部分
alert($.parseJSON(data.responseText).Message);
} }) })
</script>
</head>
</html>

Webservice代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Data; namespace WebApplication1
{
/// <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 DataSet HelloWorld(string name,string sex,string age)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("name");
dt.Columns.Add("sex");
dt.Columns.Add("age");
dt.Rows.Add(name, sex, age);
dt.Rows.Add("张三","男","");
ds.Tables.Add(dt); return ds;
}
}
}

Ajax调用WebService的更多相关文章

  1. Jquery ajax调用webservice总结

    jquery ajax调用webservice(C#)要注意的几个事项: 1.web.config里需要配置2个地方 <httpHandlers>      <remove verb ...

  2. Ajax调用WebService(一)

    Ajax调用WebService(一) http://www.cnblogs.com/leslies2/archive/2011/01/26/1934889.html 分类: Ajax 使用技术 We ...

  3. Jquery Ajax 调用 WebService

    原文:http://www.cnblogs.com/andiki/archive/2010/05/17/1737254.html jquery ajax调用webservice(C#)要注意的几个事项 ...

  4. 使用ajax调用webservice加载table

    写了个ajax调用webservice动态加载表格的案例 不废话直接上代码 webservice代码: /// <summary> /// 首页显示会员信息 /// </summar ...

  5. ASP.NET实现二维码 ASP.Net上传文件 SQL基础语法 C# 动态创建数据库三(MySQL) Net Core 实现谷歌翻译ApI 免费版 C#发布和调试WebService ajax调用WebService实现数据库操作 C# 实体类转json数据过滤掉字段为null的字段

    ASP.NET实现二维码 using System;using System.Collections.Generic;using System.Drawing;using System.Linq;us ...

  6. ajax调用webservice服务

    ajax调用是 html方向调用的, 而sqlconnection是 java代码调用的,本质差不多 <html> <head> <title>通过ajax调用we ...

  7. AJAX 调用WebService 、WebApi 增删改查

    WebService 页面: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 3 ...

  8. Ajax调用WebService接口样例

    在做手机端h5的应用时,通过Ajax调用http接口时没啥问题的:但有些老的接口是用WebService实现的,也来不及改成http的方式,这时通过Ajax调用会有些麻烦,在此记录具体实现过程.本文使 ...

  9. AJAX 调用WebService 、WebApi 增删改查(笔记)

    经过大半天努力,终于完成增删改查了!心情有点小激动!!对于初学者的我来说,一路上都是迷茫,坑!!虽说网上有资料,可动手起来却不易(初学者的我).(苦逼啊!) WebService 页面: /// &l ...

随机推荐

  1. 使用Jmeter录制脚本

    相对于LoadRunner跟SilkPerformer来说,Jmeter确实有差距,但毕竟前两者太贵,Jmeter胜在免费开源. 先看下LoadRunner录制的脚本如下,美如画,结构清晰,易于修改编 ...

  2. springMVC使用与生成序列号

    springMVC使用与生成序列号 我是以springMVC的方式提供序列号 代码可以直接在项目中用 第一步:controller类 @Autowired private PkGenerator pk ...

  3. iOS开发编译报错、常见问题(实时更新)

    一.报错与警报 1.错误代码:No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCH ...

  4. No.016 3Sum Closest

    16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium Given an array S ...

  5. uva 10340 All in All

    水题,从头到尾扫一遍就可以了,输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变),得到字符串s.例如,abcde可以得到bce,但无法得到dc. #include<a ...

  6. 建议Javascript以后都用严格模式

    建议以后都在js文件的头部加上 "use strict"; 现在主流的浏览器都支持,不支持的浏览器也会忽略掉. 可以使我们写的更规范,可控: 有些错误编译的时候就会出现,方便排错:

  7. sql基础知识(新手必备)

    一.简单查询 1.查询所有数据,查询部分列数据,列别名 SELECT * FROM 表名 SELECT 列1 AS 'BIAOTI1','BIAOTI2'=列2  FROM 表名 2.查询不重复的数据 ...

  8. 关于c#中的console用法大全

    C#之Console   Console.Write  表示向控制台直接写入字符串,不进行换行,可继续接着前面的字符写入.Console.WriteLine  表示向控制台写入字符串后换行.Conso ...

  9. windbg学习!vad

    在ring0 !address不能提供详细的信息了 可以尝试用下!vad !vad扩展显示一个或多个虚拟地址详细的虚拟地址描述符(virtual address descriptor (VAD)). ...

  10. python学习笔记2-functools.wraps 装饰器

    wraps其实没有实际的大用处, 就是用来解决装饰器导致的原函数名指向的函数 的属性发生变化的问题: 装饰器装饰过函数func, 此时func不是指向真正的func,而是指向装饰器中的装饰过的函数 i ...