前台代码:

<%@ 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. JQuery的Ajax跨域请求原理概述及实例

    今天在项目中需要做远程数据加载并渲染页面,直到开发阶段才意识到ajax跨域请求的问题,隐约记得Jquery有提过一个ajax跨域请求的解决方式,于是即刻翻出Jquery的API出来研究,发 JQuer ...

  2. nodejs生成UID(唯一标识符)——node-uuid模块

    unique identifier 惟一标识符        -->> uid 在项目开发中我们常需要给某些数据定义一个唯一标识符,便于寻找,关联. node-uuid模块很好的提供了这个 ...

  3. MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题

    ///MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题,在webconfig中配置<system.web><authentication mode ...

  4. Qlikview 处理交叉表数据

    数据来源于crossTable的时候,如何将数据做明细显示. 如图示交叉表数据 使用表格向导,选择交叉表按钮, 结果达到目的. 相关脚本. Month, 表示将要新加的字段的列明,Orders 为明细 ...

  5. zedboard如何从PL端控制DDR读写(四)

    PS-PL之间的AXI 接口分为三种:• 通用 AXI(General Purpose AXI) — 一条 32 位数据总线,适合 PL 和 PS 之间的中低速通信.接口是透传的不带缓冲.总共有四个通 ...

  6. .NET 扩展方法

    .NET 的扩展方法是在.NET 3.0引入的,MSDN给出的定义是:扩展方法使你能够向现有类型“添加”方法(包括你自定义的类型和对象噢),而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩 ...

  7. {Reship}{C#}{GDI+}GDI+画笔,线,区域类型

    =================================================================================== This article is ...

  8. Karma +Jasmine+ require JS进行单元测试并生成测试报告、代码覆盖率报告

    1. 关于Karma Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner). 该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuou ...

  9. 快考试了,尽快写完HashTable。

    (1)Count Primes 质数(素数):在大于1 的自然数中,除了1和它本身之外,不能被任何其他整数整除. 解题思路:使用一个boolean类型的数组,从i(2) 开始循环,将小于N的i的倍数都 ...

  10. RedHat下编译安装Boost

    1.解压boost_1_54_0.tar.gz 2.进入目录后,运行 ./bootstrap.sh ,会生成一个 bjam 的可执行程序 3.运行 ./bjam release install 进行编 ...