使用ASP.Net 3.5 的Ajax与Web服务开发实例
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager>
在上一篇文章(基于ASP.NET 3.5 Web Service 的JSON扩展应用)中已经讲过,JSON - JavaScript对象符号是默认序列化格式,使用它进行数据转换之间客户端服务器请求。您可以禁用所有目前启用的协议像HTTP-GET、HTTP-POST,甚至的XML格式的SOAP中使用的早期形式的Web服务。以下设置在Web.config文件同样也是这样使用。
<webServices>
<protocols>
<clear/>
</protocols>
</webServices>
</system.web>
ResponseFormat :指定是否反应将序列化的简JSON或XML 。默认值为JSON。
XmlSerializeString :指定是否所有返回类型,包括字符串类型,是为XML序列化的值将被忽略XmlSerializeString连续的响应来系列化的JSON 。
namespace AjaxWebService
{
[WebService(Namespace = "http://localhost:1382/AjaxWebService/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Service : System.Web.Services.WebService
{
string myXmlData = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Book>
<Title>Programming ASP.NET 3.5, AJAX and Web Services</Title>
</Book>";
/// <summary>
/// This method uses JSON response formatting
/// </summary>
/// <param name="months"></param>
/// <returns></returns>
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
public string getNextBackupDate(int months)
{
return DateTime.Now.AddMonths(months).ToShortDateString();
}
/// <summary>
/// This method uses XML response formatting
/// </summary>
/// <returns></returns>
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
[WebMethod]
public XmlDocument GetBookTitle()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(myXmlData);
return xmlDoc;
}
/// <summary>
/// This method uses HTTP-GET protocol to call it
/// </summary>
/// <returns></returns>
[ScriptMethod(UseHttpGet = true)]
[WebMethod]
public string HelloWorld()
{
return "Hello, world";
}
}
}
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
目前,.NET 3.5框架支持Web服务和客户端的网页可以在相同的域(同一网站)。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AJAX Web Form</title>
<script type="text/javascript">
{
AjaxWebService.Service.getNextBackupDate(1, OnSucceeded);
}
function CallHelloWorld()
{
AjaxWebService.Service.HelloWorld(OnSucceeded);
}
function CallBookTitle()
{
AjaxWebService.Service.GetBookTitle(OnSuccess, OnFail, "XmlDocument");
}
// This is the callback function that processes the Web Service return value in JSON format.
function OnSucceeded(result)
{
var myresult = document.getElementById("Text1");
myresult.value = result;
}
// This is the callback function that processes the Web Service return value in XML format.
function OnSuccess(result)
{
var myresult = document.getElementById("Text1");
myresult.value = "Title: " + result.documentElement.text;
}
// This is the callback function that processes the Web Service return value in XML format.
function OnFail(error)
{
var myresult = document.getElementById("Text1");
myresult.value = "Service Error: " + error.get_message();
}
<style type="text/css">
#Text1
{
width: 375px;
}
#Button2
{
width: 140px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service.asmx"/>
</Services>
</asp:ScriptManager>
<br />
Result: <input id="Text1" type="text" /><br />
<br />
<input id="Button1" type="button" value="Get Server Time" onclick="CallNextDate()" />
<input id="Button2" type="button" value="Say Hello World" onclick="CallHelloWorld()" />
<input id="Button3" type="button" value="Get Book Title" onclick="CallBookTitle()" />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
转载于:https://blog.51cto.com/xiaoyinnet/196073
使用ASP.Net 3.5 的Ajax与Web服务开发实例相关推荐
- asmx 接受 ajax post,jQuery ajax调用web服务(asmx)触发认证弹出框
在我的asp.net 4.0电子商务web应用程序中,登录的客户可以点击锚点,如"过去6个月内的订单"或"订单去年"来查看他过去的订单.当点击这样的锚,我做一个 ...
- ASP.NET 创建、发布、使用Web服务的详细方法
一.创建Web服务 1.首先,打开VS(本例以VS2017为例).创建一个ASP.NET Web 应用程序,位置和名称自定义,本例我把这个项目创建在桌面,名字叫helloWho.然后点击[确定],在弹 ...
- 结合使用Ajax和Web服务
什么是Web服务? Web服务是使用万维网公开应用程序功能的一种方式. 它通过使用开放协议来做到这一点,因此任何可以访问Web的使用应用程序也可以访问Web服务. 加入My developerWork ...
- ajax请求web服务返回json格式
由于.net frameword3.5以上添加了对contenttype的检查,当ajax发送请求时,如果设置了contenttype为json,那么请求webservice时,会自动将返回的内容转为 ...
- 在ASP.NET AJAX 1.0框架中访问Web服务
一. 简介 如今,微软最新推出的AJAX框架为ASP.NET AJAX 1.0(下载地址为[url]http://ajax.asp.net/downloads/default.aspx[/ur ...
- ajax在项目中怎么使用,我如何添加项目在sql中使用jQuery(ajax)通过web服务
我有一个web服务,并在其中有两种方法(select,insertdata).我想用jquery在sql中插入一条记录.我怎样才能做到这一点?我已经制作了该代码,但它不起作用.请帮助我.我如何添加项目 ...
- 第三十五 ASP.NET和Web服务(二)
*摘要 .异步Web服务 .Web服务中的事务处理 .Web服务中的缓存技术 .WSE技术 * Web服务 .Web Service是在Internet上进行分布式计算的基本构造块. .WebServ ...
- [导入]Learning.ASP.NET 2.0.with.AJAX.pdf(14.14 MB)
ASP.NET 2.0的AJAX无疑是最快,最有效,最可靠和最佳的方式支持创建交互式Web应用程序上市.结合开发工具,可以从Microsoft ,免费和商业,这是 难以置信轻松地创建网站,看看伟大的表 ...
- 微軟 ASP.NET 2.0 的 AJAX 利劍 ~ Atlas Framework
MSDN 程式開發講座: 微軟 ASP.NET 2.0 的 AJAX 利劍 - Atlas Framework 作者:奚江華 [ASP.NET網頁設計與C#]書籍作者 DotNet 開發聖殿 Blog ...
最新文章
- 如何在本地安装tomcat、jdk并且配置tomcat环境变量(window7)
- 5G NPN 行业专网 — 部署模式
- java B2B2C Springboot电子商务平台源码-Feign设计原理
- html教程padding,HTML CSS——margin和padding的学习
- python斐波那契数列30_python的30个骚操作
- java 递归遍历对象所有属性_Java学习之Xml系列二:xml按条件查询、xml递归遍历所有元素和属性...
- oracle 9i手动启动数据库,Oracle9iAS (之五): 手工起动,关闭系统
- 20211027:《Labuladong的算法小抄》学习记录(一)
- androidpn的学习研究(四)androidpn-client客户端几个类说明
- LZJ流体质量计算机价格,LZJ-ⅢC型流体质量计算机
- 网络编程 2 套接字socket
- Xamarin Mono For Android
- 为什么只看重结果_太过于看重结果会失掉过程
- 【矩阵乘】【DP】【codevs 1305】Freda的道路
- RooT最好软件,root手机最好的软件
- Android 基于Message的进程间通信 Messenger完全解析
- Database-3:SQL语句之DQL
- python:小鱼的航程
- 半导体鼻祖:仙童半导体的故事
- 蓝桥杯基础练习---特殊数字