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 ...
随机推荐
- java文件保存至服务器
import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java ...
- 【T电商 3】Nginx的Http(图片)服务器配置+ftp上传使用说明
在前两篇博客中提到了搭建Nginx和Ftp服务器,在本篇博客,主要是介绍Nginx的配置文件的使用,怎样修改配置文件使其成为一个图片服务器. 一.Nginx图片服务器配置 <span style ...
- WAMP(Windows、Apache、MySQL、php)安装配置过程常见问题
WAMP(Windows.Apache.MySQL.php)安装配置过程 可以参考该网友的总结(总结的不错,鼓掌!!): http://www.cnblogs.com/pharen/archive/2 ...
- 日志分析工具ELK配置详解
日志分析工具ELK配置详解 一.ELK介绍 1.1 elasticsearch 1.1.1 elasticsearch介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分 ...
- ComponentCount 与 ControlCount 区别
ShowMessage(panel.ComponentCount.ToString); ShowMessage(panel.ControlCount.ToString);componetcou ...
- 用tcc遇到的一个大坑
在centos6.5 x86_64服务器上编译安装完tcc, 版本0.9.25(在github上clone的),似乎一切正常 但当用tcc来编译"hello, world"程序时, ...
- DIOCP之数据接收事件
一.不引用编码器与解码器的情况下(ECHO的DEMO) 类TIOCPtcpclient,接收服务器的数据事件:OnRecvBuffer 类TDiocpTcpServer,接收客户端数据事件:OnRec ...
- asp.net GDI+把图片绘制成自定义的椭圆形状
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- POJ 1228 - Grandpa's Estate 稳定凸包
稳定凸包问题 要求每条边上至少有三个点,且对凸包上点数为1,2时要特判 巨坑无比,调了很长时间= = //POJ 1228 //稳定凸包问题,等价于每条边上至少有三个点,但对m = 1(点)和m = ...
- 《机器学习实战》学习笔记——第13章 PCA
1. 降维技术 1.1 降维的必要性 1. 多重共线性--预测变量之间相互关联.多重共线性会导致解空间的不稳定,从而可能导致结果的不连贯.2. 高维空间本身具有稀疏性.一维正态分布有68%的值落于正负 ...