ajax简单应用之传递数据的方式

1.利用XMLHttpRequest对象

前台代码:

function createAjax() {

             var xmlhttp;

             if (window.ActiveXObject)

                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //对ie浏览器创建

             else if (window.XMLHttpRequest)

                 xmlhttp = new XMLHttpRequest(); //对其他浏览器创建

             return xmlhttp;

         }

        

         function ajax() {

            var string1 = "北京时间";

            var string2 = "是:";

             var xmlhttp = createAjax(); //创建

             xmlhttp.open("post", "Ajax/ServerTime.ashx", true); //get/post,  url  ,  是否异步传输

             xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //post方式必须加上的一句

             xmlhttp.onreadystatechange = function () {    //状态变化时执行的函数

                 

                 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                     var respon = xmlhttp.responseText;

                     document.getElementById("time").innerHTML = xmlhttp.responseText;

                 }



             }

             xmlhttp.send("string1="+string1+"&string2="+string2);//传递数据到一般处理程序

             window.setTimeout(ajax, 100);

         }

   一般处理程序中:

 public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            string string1 = context.Request.Form["string1"].Trim();

            string string2 = context.Request.Form["string2"].ToString();

            context.Response.Write(string1+string2+DateTime.Now.ToLongTimeString());

            context.Response.End();

        }

2.调用cs里面的方法

HTML中的代码

function gettime() {

             var a="北京时间是:"

            $.ajax({

                 type: "post",

                 url: "headMuLu.aspx/gettime",

                 dataType: "Json",

                 contentType: "application/json; charset=utf-8",

                 data: "{a:'" + a + "'}",//传参数

                 //async: false,

                 success: function (result) {

                     document.getElementById("time").innerHTML = result;

                 },

                 error: function () {

                     // $("#ajax").html("请求失败!");

                     Strhtml = "请求失败";

                     rtStr = Strhtml;

                 }

             });

             window.setTimeout(gettime, 100);

         }

cs文件中的代码

        [WebMethod]

        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]

        public static string gettime(string a)

        {

            return a+DateTime.Now.ToLongTimeString();

        }

1.引用jquery.form.js插件再利用表单提交到一般处理程序

     form1是form的id,利用表单能使传值变简单,能在一般处理程序中通过context.Request.Form["UserName"].Trim()来获取页面中值具有name属性的值

       HTML中的代码

function time() {
             $('#form1').ajaxSubmit({
                 url: "Ajax/time.ashx",
                 dataType: "text",
                 contentType: "application/json; charset=utf-8",
                 data: {},
                 success: function (data) {
                     document.getElementById("time").innerHTML = data;
                 },
                 error: function (e) {
                     alert("系统错误");
                 }
             });

         }

<input type="text"  name="div1"  style=" visibility: hidden" value="你过来保证不打死你"  />
 <input type="text"  name="div2" style=" visibility: hidden" value="我真没打死你" />

一般处理程序中:

context.Response.ContentType = "text/plain";

            string string1 = context.Request.Form["div1"];

            string string2 = context.Request.Form["div2"];

            Random rd = new Random();

            int a=Convert.ToInt32( rd.Next(0,2));

            if (a == 0)

            {

                context.Response.Write(string1);

            }

            else {

                context.Response.Write(string2);

            }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值