您的位置:首页 > 移动开发

Mobile App VS .NET Communication Through Ajax

2015-06-19 16:50 260 查看
Client Side(Web App)

<script src="js/mui.min.js"></script>

<button onclick="GetWithCallback()">测试jquery.get</v></button><br/>
<button onclick="PostWithCallback()">测试jquery.post</v></button><br/>

<script>

       //测试get,使用回调函数  

        //注意:get()方法提供了回调函数(callback),该函数有三个参数,分别代表请求返回的内容、请求状态和XMLHttpRequest对象  

        function GetWithCallback() {  

  

            $.get("http://222.178.71.253:8033/Default.aspx", { "param": "TestGet-Callback" }, function (responseText, textStatus, XMLHttpRequest) {  

  

                $("#result").html("回调函数在起作用,结果:" + responseText);  

  

            });  

        }  

  

        //测试post,使用回调函数  

        function PostWithCallback() {  

  

            $.post("http://222.178.71.253:8033/Default.aspx", { "param": "TestPost-Callback" }, function (responseText, textStatus, XMLHttpRequest) {  

  

                $("#result").html("回调函数在起作用,结果:" + responseText);  

  

            });  

        }  

</script>

.NET side

client

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"

    CodeBehind="Default.aspx.cs" Inherits="GetMethodInfo._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <h2>

        Welcome to ASP.NET!

    </h2>

    <p>

        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.

    </p>

    <p>

        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"

            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.

    </p>

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</asp:Content>

server

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

namespace GetMethodInfo

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            string param = "";

            //获取参数  

            if (!String.IsNullOrEmpty(HttpContext.Current.Request["param"]))

            {

                param = HttpContext.Current.Request["param"];

            }

            //清空缓冲区  

            Response.Clear();

            //将字符串写入响应输出流  

            Response.Write("请求的方式为:" + Request.HttpMethod.ToUpper() + "; 传递过来的参数为:" + param);

            //将当前所有缓冲的输出发送的客户端,并停止该页执行  

          //  Response.End();

            string fileLoc = @"d:\sample1.txt";

            FileStream fs = null;

            if (!File.Exists(fileLoc))

            {

                using (fs = File.Create(fileLoc))

                {

                    using (StreamWriter sw = new StreamWriter(fileLoc))

                    {

                        sw.Write(param);

                    }

                }

            }

            Label1.Text = param+" "+DateTime.Now.ToString();

           // Response.End();

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: