前台代码:

<%@ 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. js处理用户输入的银行卡号实现四位一段,并且只能输入数字

    其中ABC是输入框的id.type="tel"可直接呼出数字键盘. <input type="tel" maxlength="24" ...

  2. Kettle6使用

    1.Kettle是一个开源的ETL(Extract-Transform-Load的缩写,即数据抽取.转换.装载的过程)项目,java编写,绿色无需安装 下载http://community.penta ...

  3. delphi action学习

    procedure TEditAction.UpdateTarget(Target: TObject); begin if (Self is TEditCut) then Enabled := (Ge ...

  4. C#中combobox 控件属性、事件、方法

    一 .combobox 属性.事件.方法公共属性 名称 说明 AccessibilityObject 获取分配给该控件的 AccessibleObject. AccessibleDefaultActi ...

  5. angularjs 延迟更新和angularjsUI

    angularjsUI库https://material.angularjs.org/latest/ ng-model-options="{ updateOn: 'blur' }" ...

  6. 详解log4j2(上) - 从基础到实战

    log4j2相对于log4j 1.x有了脱胎换骨的变化,其官网宣称的优势有多线程下10几倍于log4j 1.x和logback的高吞吐量.可配置的审计型日志.基于插件架构的各种灵活配置等.如果已经掌握 ...

  7. C++中 容易忽视的const 修饰符

    C++可以用const定义常量,也可以用#define定义常量,但是前者比后者有更多的有点: (1)const常量有数据类型,而宏常量没有数据类型.编译器可以对const进行类型安全检查,而后者只进行 ...

  8. oracle SQL查询中间若干条记录

    方法一:利用rownum和差集函数minus select * from ( select * from emp order by sal) where rownum<13 minus sele ...

  9. 泛型的上限和下限的Demo

    Main Class package Comparator.Bean; import java.math.BigDecimal; import java.util.List; import java. ...

  10. RESULT:0x80029C4A (TYPE_E_CANTLOADLIBRARY))

    无法将类型为"Microsoft.Office.Interop.Excel.ApplicationClass"的 COM 对象强制转换为接口类型"Microsoft.Of ...