Asp.Net开发的WebService输出JSON格式
2012-07-27 16:53
323 查看
本已经做好的Asp.net 2.0 to WebService,客户需要输出JSON的格式(不要SOAP XML),只能进行修改。
修改步骤如下:
1、给VS2005安装ASPAJAXExtSetup.msi,Ext中存放了JSON格式化的方法。
2、项目中增加引用System.Web.Extensions (.Net)。
3、Web.Config 中在<system.web>下增加配置节点 :
(1)using
用Html去调用JSON格式的WebService
修改步骤如下:
1、给VS2005安装ASPAJAXExtSetup.msi,Ext中存放了JSON格式化的方法。
2、项目中增加引用System.Web.Extensions (.Net)。
3、Web.Config 中在<system.web>下增加配置节点 :
<httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </httpHandlers> <webServices> <protocols> <add name="HttpPost"/> <add name="HttpGet"/> </protocols> </webServices>4、在Service.cs中相应修改。
(1)using
using System; using System.Web; using System.Web.Services; using System.Web.Script.Services; using System.Web.Script.Serialization; using System.Collections; using System.Collections.Generic; using System.Data.SqlClient; using System.Data;(2)Namespace
[WebService(Namespace = "http://XXXXXXXXXX/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [ScriptService] public class Service : System.Web.Services.WebService {......}(3)Function
[WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string GetMobileVerificationCode(string MobilePhone) {return new JavaScriptSerializer().Serialize("1".ToString());}
用Html去调用JSON格式的WebService
<script type="text/javascript" src="jquery-1.2.6.js"></script> <script type="text/javascript"> function testJson() { $.ajax({ type: "POST", url: "Service.asmx/GetMobileVerificationCode", data: "{MobilePhone:1}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { var data = eval("(" + msg + ")"); //alert(data); var t = "<div>"; t = t + "<span>" + data.MemberID + "</span> <span>" + data.MemberName + "</span> <span>" + data.MemberMobile + "</span><br />"; t = t + "</div>"; $("#jsonDiv").html(t); }, error: function(msg){ } }); } </script> <div><input type="button" value="Json" onclick="testJson()" /></div> <div id="jsonDiv" class="jsonDiv"></div>
相关文章推荐
- ASP.NET Hashtable输出JSON格式数据 - 贵源网络 - 博客园
- ASP.NET输出Json格式数据
- ASP.NET输出JSON格式数据
- ASP.NET Hashtable输出JSON格式数据
- ASP.NET输出JSON格式数据
- ASP.NET Hashtable输出JSON格式数据
- 在ASP.NET中使用ContentType="类型名";来确定输出格式
- C#开发的WebService使用JSON格式传递数据+Ajax测试
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式问题的方法
- asp.net(c#)快速生成Json格式的数据(JsonHelper)
- ASP.NET 2.0 里输出文本格式流
- iOS开发之结合asp.net webservice实现文件上传下载
- Delphi开发WebService给ASP.NET调用
- 一步一步使用Ext JS MVC与Asp.Net MVC 3开发简单的CMS后台管理系统之创建输出验证码图片的控制器
- 使用ASP.NET开发WAP强制输出WML
- Asp.Net_Ajax调用WebService返回Json前台获取循环解析
- Newtonsoft.Json 为asp.net 3.5开发的
- ASP.NET DataSet查询结果转换为JSON格式数据
- 【转】开发基于ASP.NET WebService的图片验证码服务
- ASP.NET Web API 配置返回的json字段的格式以及Action返回HttpResponseMessage类型