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'}",
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的更多相关文章
- Jquery ajax调用webservice总结
jquery ajax调用webservice(C#)要注意的几个事项: 1.web.config里需要配置2个地方 <httpHandlers> <remove verb ...
- Ajax调用WebService(一)
Ajax调用WebService(一) http://www.cnblogs.com/leslies2/archive/2011/01/26/1934889.html 分类: Ajax 使用技术 We ...
- Jquery Ajax 调用 WebService
原文:http://www.cnblogs.com/andiki/archive/2010/05/17/1737254.html jquery ajax调用webservice(C#)要注意的几个事项 ...
- 使用ajax调用webservice加载table
写了个ajax调用webservice动态加载表格的案例 不废话直接上代码 webservice代码: /// <summary> /// 首页显示会员信息 /// </summar ...
- 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 ...
- ajax调用webservice服务
ajax调用是 html方向调用的, 而sqlconnection是 java代码调用的,本质差不多 <html> <head> <title>通过ajax调用we ...
- 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 ...
- Ajax调用WebService接口样例
在做手机端h5的应用时,通过Ajax调用http接口时没啥问题的:但有些老的接口是用WebService实现的,也来不及改成http的方式,这时通过Ajax调用会有些麻烦,在此记录具体实现过程.本文使 ...
- AJAX 调用WebService 、WebApi 增删改查(笔记)
经过大半天努力,终于完成增删改查了!心情有点小激动!!对于初学者的我来说,一路上都是迷茫,坑!!虽说网上有资料,可动手起来却不易(初学者的我).(苦逼啊!) WebService 页面: /// &l ...
随机推荐
- DOCTYPE 中xhtml 1.0和 html 4.01区别分析
前者相对于后者有以下特性: 1.所有的标记都都要闭合 所有的标记都要闭合,如果是单独不成对的标签,在标签最后加一个"/"来关闭它.例如: <h6>close tag & ...
- delphi 怎么获取工程版本号
function GetApplicationVersion:String; // Added 取得程序版本号 var FileName:String; InfoSize,Wnd:DWORD; Ver ...
- NHibernate系列文章九:NHibernate对象二级缓存上
摘要 NHibernate的二级缓存由SessionFactory管理,由所有Session共享. NHibernate缓存读取顺序: 首先从一级缓存中读取,如果一级缓存对象存在,则读取一级缓存对象并 ...
- NAT协议
NAT服务器的设定 NAT的全名:Network Address Translation;即网络地址的转换: iptables指令就能够修改IP封包的表头数据,IP的目标地址,源地址都可以修改. ...
- Objective c, +load, +initialize 方法
+load() 当类被加载入程序的时候会执行+load方法 +initialize() 当类第一次被使用的时候会执行+initialize方法 这两个方法都只会被执行一次.
- Java项目导出war包 security alert:integrity check error”
操作方法: 首先下载这个jar包 http://pan.baidu.com/s/1sk5uDzf 1.需要先把common/pluns 下的com.genuitec.eclipse.export.wi ...
- vbs获取命令行里的参数
var args1=WScript.Arguments.Item(0) var args2=WScript.Arguments.Item(1)
- 界面设计常用CSS属性
CSS常用属性整理: 1 字体属性 font-family 设置使用的字体 font-style 设置字体的样式,是否斜体 font-variant 设置字体的大小写 font-weight 设置字体 ...
- 查看真机的APP沙盒文件
1.Xcode --> window --> devices -->左边选择设备 右下边选择要查看的app 双击应用可查看目录 点击设置按钮,选 Download Container ...
- Activity生命周期(二)
------siwuxie95 在项目 ActivityLifeCircle 的 MainActivity.java 中添加方法: onCreate() onStart() onResume() ...