积少成多Flash(3) - ActionScript 3.0 基础之以文本形式、XML形式和JSON形式与ASP.NET通信

简介:
+关注继续查看
[索引页]
[源码下载]


积少成多Flash(3) - ActionScript 3.0 基础之以文本形式、XML形式和JSON形式与ASP.NET通信


作者:webabcd


介绍
Flash ActionScript 3.0 以文本形式与ASP.NET通信、以XML形式与ASP.NET通信和以JSON形式与ASP.NET通信


示例
Text.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class Text : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                string s = "name: " + Request.QueryString["name"] + "; age: " + Request.QueryString["age"]; 
InBlock.gif 
InBlock.gif                Response.ClearContent(); 
InBlock.gif                Response.ContentType = "text/plain"
InBlock.gif                Response.Write(s); 
InBlock.gif                Response.End(); 
InBlock.gif        } 
InBlock.gif}
 
Xml.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class Xml : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                string s = @"<?xml version=""1.0"" encoding=""utf-8""?> 
InBlock.gif                        <root> 
InBlock.gif                                <person name=""webabcd"" age=""27""> 
InBlock.gif                                        <salary>1000</salary> 
InBlock.gif                                </person> 
InBlock.gif                                <person name=""webabcdefg"" age=""37""> 
InBlock.gif                                        <salary>2000</salary> 
InBlock.gif                                </person> 
InBlock.gif                                <person name=""webabcdefghijklmn"" age=""47""> 
InBlock.gif                                        <salary>3000</salary> 
InBlock.gif                                </person> 
InBlock.gif                        </root>"; 
InBlock.gif 
InBlock.gif                Response.ClearContent(); 
InBlock.gif                Response.ContentType = "text/xml"
InBlock.gif                Response.Write(s); 
InBlock.gif                Response.End(); 
InBlock.gif        } 
InBlock.gif}
 
JSON.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gif 
InBlock.gifpublic partial class JSON : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                Person person = new Person(); 
InBlock.gif                person.Name = "webabcd"
InBlock.gif                person.Age = 27; 
InBlock.gif 
InBlock.gif                HttpContext.Current.Response.ClearContent(); 
InBlock.gif                // HttpContext.Current.Response.ContentType = "application/json"; 
InBlock.gif                HttpContext.Current.Response.ContentType = "text/plain"
InBlock.gif 
InBlock.gif                // 把person对象序列化成JSON 
InBlock.gif                System.Runtime.Serialization.DataContractJsonSerializer dcjs = new System.Runtime.Serialization.DataContractJsonSerializer(person.GetType()); 
InBlock.gif                dcjs.WriteObject(HttpContext.Current.Response.OutputStream, person); 
InBlock.gif 
InBlock.gif                HttpContext.Current.Response.End(); 
InBlock.gif        } 
InBlock.gif
InBlock.gif 
/// <summary> 
/// Person类 
/// </summary> 
InBlock.gif[System.Runtime.Serialization.DataContract] 
InBlock.gifpublic class Person 
InBlock.gif
InBlock.gif        private string _name; 
InBlock.gif        /// <summary> 
InBlock.gif        /// 姓名 
InBlock.gif        /// </summary> 
InBlock.gif        [System.Runtime.Serialization.DataMember] 
InBlock.gif        public string Name 
InBlock.gif        { 
InBlock.gif                get { return _name; } 
InBlock.gif                set { _name = value; } 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        private int _age; 
InBlock.gif        /// <summary> 
InBlock.gif        /// 年龄 
InBlock.gif        /// </summary> 
InBlock.gif        [System.Runtime.Serialization.DataMember] 
InBlock.gif        public int Age 
InBlock.gif        { 
InBlock.gif                get { return _age; } 
InBlock.gif                set { _age = value; } 
InBlock.gif        } 
InBlock.gif}
 
Net.as
package 

        import flash.display.Sprite; 
        import flash.net.URLLoader; 
        import flash.net.URLRequest; 
        import flash.net.URLVariables; 
        import flash.net.URLRequestMethod; 
        import flash.events.Event; 
         
        // 对JSON的支持 
        import com.adobe.serialization.json.JSON; 
         
        public class Net extends Sprite 
        { 
                public function Net() 
                { 
                        // 以文本形式与ASP.NET通信 
                        showText(); 
                         
                        // 以XML形式与ASP.NET通信 
                        showXml(); 
                         
                        // 以JSON形式与ASP.NET通信 
                        showJSON(); 
                } 
                 
                // 以文本形式与ASP.NET通信 
                function showText():void 
                { 
                        var v:URLVariables = new URLVariables("name=webabcd&age=27"); 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/Text.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                        var l:URLLoader = new URLLoader(); 
                        l.load(r); 
                        l.addEventListener(Event.COMPLETE, textCompleteHandler); 
                } 
                 
                function textCompleteHandler(event:Event):void 
                { 
                        var l:URLLoader = URLLoader(event.target); 
                         
                        trace(l.data); 
                        // output: name: webabcd; age: 27 
                } 
                 
                // 以XML形式与ASP.NET通信 
                function showXml():void 
                { 
                        var v:URLVariables = new URLVariables() 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/Xml.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                         
                        var l:URLLoader = new URLLoader(); 
                        l.load(r); 
                        l.addEventListener(Event.COMPLETE, xmlCompleteHandler); 
                } 
                 
                function xmlCompleteHandler(event:Event):void 
                { 
                        var l:URLLoader = event.target as URLLoader; 
                        var xml:XML = new XML(l.data); 
                         
                        for each(var v in xml.person) 
                        { 
                                trace("姓名:" + v.@name + ";年龄:" + v.@age + ";薪水:" + v.salary); 
                        } 
                        // output:    
                        // 姓名:webabcd;年龄:27;薪水:1000 
                        // 姓名:webabcdefg;年龄:37;薪水:2000 
                        // 姓名:webabcdefghijklmn;年龄:47;薪水:30 
                } 
                 
                // 以JSON形式与ASP.NET通信 
                function showJSON():void 
                { 
                        var v:URLVariables = new URLVariables() 
                        var r:URLRequest = new URLRequest(); 
                        r.url = "http://localhost:1343/Web/JSON.aspx"; 
                        r.method = URLRequestMethod.GET; 
                        r.data = v; 
                         
                         
                        var l:URLLoader = new URLLoader(); 
                        l.load(r); 
                        l.addEventListener(Event.COMPLETE, jsonCompleteHandler); 
                } 
                 
                function jsonCompleteHandler(event:Event):void 
                { 
                        var l:URLLoader = event.target as URLLoader; 
                         
                        var v:* = JSON.decode(l.data); 
                         
                        trace("姓名:" + v.Name + ";年龄:" + v.Age); 
                        // output: 姓名:webabcd;年龄:27 
                } 
        } 
}
 
 





     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/342184,如需转载请自行联系原作者

相关文章
|
开发框架 前端开发 .NET
ASP.NET MVC 中使用Highcharts+Ajax+Json生成动态曲线图,柱状图,饼图
ASP.NET MVC 中使用Highcharts+Ajax+Json生成动态曲线图,柱状图,饼图
108 0
ASP.NET MVC 中使用Highcharts+Ajax+Json生成动态曲线图,柱状图,饼图
|
JSON 开发框架 前端开发
ASP.NET MVC中JSON强制小写属性名称
首先需要引用Newtonsoft.Json using Newtonsoft.Json; using Newtonsoft.Json.Serialization; 然后转换对象 Model.JsonResult jsonResult = new Model.
872 0
|
JSON .NET 数据格式
2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」
原文:2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」 希望给你3-5分钟的碎片化学习,可能是坐地铁、等公交,积少成多,水滴石穿,谢谢关注。 命令行配置  1.新建控制台项目  2.
1100 0
|
JSON .NET 数据格式
2.1命令行和JSON的配置「深入浅出ASP.NET Core系列」
希望给你3-5分钟的碎片化学习,可能是坐地铁、等公交,积少成多,水滴石穿,谢谢关注。 命令行配置  1.新建控制台项目  2.nuget引入microsoft.aspnetcore.all  这里要注意版本号必须和dotnetcore兼容 3.
778 0
|
JSON 前端开发 .NET
如何构建ASP.NET MVC4&JQuery&AJax&JSon示例
背景:   博客中将构建一个小示例,用于演示在ASP.NET MVC4项目中,如何使用JQuery Ajax。 步骤: 1,添加控制器(HomeController)和动作方法(Index),并为Index动作方法添加视图(Index.cshtml),视图中HTML如下: 输入你的姓名: 输入你的年龄: 提交 清空   视图中包含两个文本框,分别用来输入名字和年龄,包含连个按钮,分别用来提交信息和清空文本框的内容,同时包含一个段落,用来显示Ajax返回的数据信息。
1030 0
|
JSON .NET 数据格式
Asp.net MVC5 返回json数据忽略序列化属性
在属性上添加 [ScriptIgnore] 特性,命名空间是System.Web.Script.Serialization用心做好每一件事,结果会给你最大的惊喜!
1069 0
|
JSON 前端开发 .NET
菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample   可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来,我们可以直接添加Microsoft.
1716 0
|
JSON .NET C#
ASP.NET CORE入门之读取Json配置文件
首先新建一.net core控制台项目,命名为jsonReader 然后选中引用,选择NuGet包管理器,点击浏览引入mircosoft.aspnetcore.all并安装 选中解决方案,填加,新建项目,添加Json文件,命名为test.
1374 0