码迷,mamicode.com
首页 > Web开发 > 详细

ajax异步提交

时间:2016-02-18 08:49:40      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

需引入jquery脚本

1、提交端脚本

var eid = ‘<%= str_eid%>‘;
        var date = ‘<%= str_date%>‘;
        var year = ‘<%= str_year%>‘;
        var quarter = ‘<%= str_quarter%>‘;
        var ssid = ‘<%= str_ssid%>‘;

        //队列提交开始
        var ajaxes = [];
        var xmlhttp = function () {
            var xhr = false;
            try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e1) {
                    xhr = false;
                }
            }
            if (!xhr && typeof XMLHttpRequest != ‘undefined‘) {
                xhr = new XMLHttpRequest();
            }
            return xhr;
        }


        var xhr = new xmlhttp(); //获得XMLHttpRequest实例对象xhr
        //executeAjax是主要的执行Ajax的函数
        var executeAjax = function () {
            //            //如果队列为空,则退出执行
            if (!ajaxes.length)
                return;

            //setTimeout("waitTime()", 2000);
            var options = ajaxes[0];
            if (xhr) {
                xhr.open(options.method, options.url, true);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304)) {
                        options.callback(xhr.responseText, options.deptid, options.scid);
                        //删除队列中的第一个请求
                        ajaxes.shift();
                        //如果队列中还有请求,就接着递归执行executeAjax函数,直到队列为空
                        if (ajaxes.length > 0) {
                            executeAjax();
                        }
                    }
                }
                if (xhr.method === "post") {
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                }
                xhr.send(options.data || null);
            }
        }
        //用于添加队列的函数
        var addAjax = function (options) {
            ajaxes.push(options);
        }


        //具体的操作
        var submitOneQuestion = function (deptid, scid, levelid, score) {
            var url = String.format("mz_exam2Operation.ashx?eid={0}&&date={1}&&year={2}&&quarter={3}&&ssid={4}&&deptid={5}&&scid={6}&&levelid={7}&&score={8}", eid, date, year, quarter, ssid, deptid, scid, levelid, score);
            addAjax({ method: "GET", url: url, deptid: deptid, scid: scid, callback: applyDom });
            //开始执行队列
            executeAjax();
        }

        //返回状态
        var applyDom = function (reponseText, deptid, scid) {
            //reloadNum();
            if (reponseText == "success") {
                var cell_rbtn1 = $(‘#‘ + ‘cell_‘ + deptid + ‘_‘ + scid + ‘_rbtn1‘);
                var cell_rbtn2 = $(‘#‘ + ‘cell_‘ + deptid + ‘_‘ + scid + ‘_rbtn2‘);
                var cell_rbtn3 = $(‘#‘ + ‘cell_‘ + deptid + ‘_‘ + scid + ‘_rbtn3‘);
                var cell_score = $(‘[id^=cell_‘ + deptid + ‘_‘ + scid + ‘_score]‘);

                cell_rbtn1.css(‘color‘, ‘#6bd553‘);
                cell_rbtn2.css(‘color‘, ‘#6bd553‘);
                cell_rbtn3.css(‘color‘, ‘#6bd553‘);
                //cell_score.css(‘color‘, ‘#6bd553‘);


                cell_rbtn1.css(‘fontWeight‘, ‘bold‘);
                cell_rbtn2.css(‘fontWeight‘, ‘bold‘);
                cell_rbtn3.css(‘fontWeight‘, ‘bold‘);
                //cell_score.css(‘fontWeight‘, ‘bold‘);

                cell_score.attr(‘isRequest‘, ‘1‘);//是否有返回值,0为未返回,1为返回。
            }
        }


        //队列提交结束
        function RadioChanged(cname, classno) {
            $("#" + cname).attr("level", classno);

            var deptid = cname.split(‘_‘)[1];
            var scid = cname.split(‘_‘)[2];
            var levelid = classno;
            var score = "0";

            //不同类型有不同分数段
            if (scid >= 1 && scid <= 4) {
                switch (classno) {
                    case "1":
                        $("#" + cname).val("15"); score = 15; break;
                    case "2":
                        $("#" + cname).val("10"); score = 10; break;
                    case "3":
                        $("#" + cname).val("5"); score = 5; break;
                    default:
                        $("#" + cname).val("15"); score = 15; break;
                }
            }
            else
                if (scid >= 5 && scid <= 8) {
                    switch (classno) {
                        case "1":
                            $("#" + cname).val("10"); score = 10; break;
                        case "2":
                            $("#" + cname).val("7"); score = 7; break;
                        case "3":
                            $("#" + cname).val("3"); score = 3; break;
                        default:
                            $("#" + cname).val("10"); score = 10; break;
                    }
                }

            //增加随时提交。
            submitOneQuestion(deptid, scid, levelid, score);
        }

2、接收端代码

public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";


        String str_eid = context.Request.QueryString["eid"].ToString();
        String str_date = context.Request.QueryString["date"].ToString();
        String str_year = context.Request.QueryString["year"].ToString();
        String str_quarter = context.Request.QueryString["quarter"].ToString();
        String str_ssid = context.Request.QueryString["ssid"].ToString();


        String str_deptid = context.Request.QueryString["deptid"].ToString();
        String str_scid = context.Request.QueryString["scid"].ToString();
        String str_levelid = context.Request.QueryString["levelid"].ToString();
        String str_score = context.Request.QueryString["score"].ToString();


        if (SubmitOneQuestion(str_eid, str_date, str_year, str_quarter, str_ssid, str_deptid, str_scid, str_levelid, str_score))
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("success");
        }
        else
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("failed");
        }
        context.Response.Buffer = true;
        context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        context.Response.Cache.SetExpires(DateTime.Now.AddDays(-1));
        context.Response.Expires = 0;
        context.Response.CacheControl = "no-cache";
        context.Response.Cache.SetNoStore();


    }

 

ajax异步提交

标签:

原文地址:http://www.cnblogs.com/loge/p/5197045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!