首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

ajax异步请求有关问题(ajax回调中调用ajax,是同步还是异步)

2012-12-21 
ajax异步请求问题(ajax回调中调用ajax,是同步还是异步)本帖最后由 cyw1592951 于 2012-11-20 15:56:45 编

ajax异步请求问题(ajax回调中调用ajax,是同步还是异步)
本帖最后由 cyw1592951 于 2012-11-20 15:56:45 编辑 场景:
三个ajax请求,1:查询用户名,2:初始用户业务数据,3:登录验证
用户登录时 输入用户名之后(使用1),查询出用户的时候,开始使用(2)ajax到后台去初始用户的业务数据;
输入用户名点击登录之后 验证(使用3)通过直接跳转;
目前情况:点击 登录之后要等 很久 个人认为是 等待 2  执行完成之后才跳转
要求:验证通过直接跳转

ajax代码如下


window.LCount = 0;
$(function () {
    var gongHao = $('#txtNO'), uid = $('#txtUid'), pwd = $('#txtPwd');
    gongHao.bind('keydown', function (e) {
        var key = e.which;
        if (key == 13) {
            if (checkEmpty(1)) {
                pwd.focus();
            }
        }
    });
    pwd.bind('keydown', function (e) {
        var key = e.which;
        if (key == 13) {
            $("#btnLogin").click();
        }
    });

    $('#btnLogin').bind('keydown', function (e) {
        var key = e.which;
        if (key == 13) {
            $("#btnLogin").click();
        }
    });
    function action(rValue) {
        var result = eval("(" + rValue + ")");
        if (result.status == 0) {
            uid.val(result.message);
            pwd.focus();

        }
    };

    function checkEmpty(e) {
        if ($.trim(gongHao.val()) == '') {
            $('#btnLogin').val("登 录");
            alert("工号不能为空!");
            gongHao.focus();
            return false;
        }

        else if (e == 2) {
            if ($.trim(pwd.val()) == '') {
                $('#btnLogin').val("登 录");
                alert("密码不能为空!");
                pwd.focus();
                return false;


            }
        }
        return true;
    };
    gongHao.change(function () {
        var Result, RState;
        if (checkEmpty(1)) {
            uid.val('正在读取用户名...');
            $.ajax({
                global: false,
                async: true,
                url: 'login.aspx?GH=' + gongHao.val() + "&_" + Math.random(),
                success: function (Result) {
                    var result = eval("(" + Result + ")");
                    if (result.status == 0) {
                        uid.val(result.message);
                        pwd.focus();
                        $.ajax({
                            global: false,
                            cache: false,
                            async: true,
                            // type: 'POST',
                            url: 'Login.aspx?action=Ready&SFZ=' + gongHao.val() + "&_" + Math.random(),
                            success: function (RState) {
                            }
                        });
                       
                    }
                    else if (result.status == -1) {


                        alert(result.message);
                        gongHao.focus();

                    }
                }
            });
           
        }
    });

    $('#btnLogin').click(function () {
        var Result;
        $('#btnLogin').val("登录中...");
        if (!checkEmpty(2)) {
            return false;
        } else {
            $.ajax({
                global: false,
                async: true,
                url: "login.aspx?GH=" + gongHao.val() + "&Pwd=" + pwd.val(),
                success: function (Result) {
                    var result = eval("(" + Result + ")");
                    if (result.status == 1) {
                        top.window.location.href = 'Default.aspx?Model=0';
                    }
                    else if (result.status == -2) {
                        pwd.val('');
                        $('#btnLogin').val("登 录");
                        alert(result.message);
                        pwd.focus();
                        return false;
                    }
                }
            });

        }


    });
});

window.onload = function () {
    document.getElementById("txtNO").focus();
}



[解决办法]
C#代码


using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using MediInfo.Extensions;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.IO;
using System.Data;
using System.Threading;

namespace MediInfo.EHR_VIEW.UIL
{
    public partial class Login : BasePage
    {
        IBLL.IEHR_MPI Bll = new BLL.EHR_MPI();
        IBLL.IBLL_Common CommanBll = new BLL.BLL_Common();
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Request["Model"] != null && Request["Model"]=="1")
            {
                if (Request["MPI"] != null && Request["MPI"].ToString() != "")
                {
                   //此部分暂且去掉,
                }
            } 
            else if (!Page.IsPostBack)
            {
                if (Request["action"] != null)
                {
                    if (!string.IsNullOrEmpty(Request["action"]))
                    {
                        switch (Request["action"].ToString().ToLower())
                        {
                            case "ready":

                                string DataLogic = ConfigurationManager.AppSettings["DataLogic"].ToString();
                                int State = 0;


                                switch (DataLogic) { 
                                    case "1":
                                        if (Request["SFZ"] != null)
                                        {
                                            if (!string.IsNullOrEmpty(Request["SFZ"]))
                                            {
                                                string ShengFenZh = Request["SFZ"].ToString().Trim();

                                                if (!CommanBll.AccessOver(CookieInfo.MPI.ToString()))
                                                {
                                                    CommanBll.READY_YONGHUSJ(ShengFenZh, State);
                                                     CookieInfo.DataRedy = "True";
                                                }
                                                else
                                                {


                                                      CookieInfo.DataRedy = "True";
                                                }
                                                Response.Clear();
                                                Response.Write("{status:" + State.ToString() + "}");
                                                Response.End();
                                            }
                                        }
                                        break;
                                    case "0":
                                        Response.Clear();
                                        Response.Write("{status:" + State.ToString() + "}");
                                        Response.End();
                                        break;                                
                                }                               


                                break;
                        }

                    }
                }
                else
                {
                    AJAX_Action();
                    Response.Cookies.Add(new HttpCookie("testCookie", ""));
                }
              
            }
        }
      
        private void ReadyDataPro(object MPI)
        {
            int p = 0;
            string DataReady = "false";
            if (!CommanBll.AccessOver(MPI.ToString()))
            {
                CommanBll.READY_YONGHUSJBYMPI(MPI.ToString(), p);
                DataReady = "True";
                // CookieInfo.DataRedy = "True";
                //Session["DataReady"] = "True";
            }
            else
            {
                DataReady = "True";
            }
            //Session["DataReady"] = DataReady;
            CookieInfo.DataRedy = DataReady;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="mpi"></param>
        private bool GetYongHuXXInnerP(string mpi)
        {           


            bool b = true;       
            if (mpi != CookieInfo.MPI)
            {
                DataSet ds = CommanBll.Get_EHR_YONGHULOGIN("and MPI='" + mpi + "'");

                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {                 
                    CookieInfo.SHENFENZH = ds.Tables[0].Rows[0]["SHENFENZH"].ToString();
                    CookieInfo.XINGMING = ds.Tables[0].Rows[0]["XINGMING"].ToString();
                    CookieInfo.MPI = ds.Tables[0].Rows[0]["MPI"].ToString();
                    CookieInfo.XINGBIE = ds.Tables[0].Rows[0]["XINGBIEMC"].ToString();
                    CookieInfo.DYZ_USERID = ds.Tables[0].Rows[0]["SHENFENZH"].ToString();
                    CookieInfo.DYZ_USERNAME = ds.Tables[0].Rows[0]["XINGMING"].ToString();
                    CookieInfo.CHUSHENGRQ = ds.Tables[0].Rows[0]["CHUSHENGRQ"].ToString();
                    FormsAuthentication.SetAuthCookie(CookieInfo.MPI, false);
                    b = true;
                }
                else
                {

                    Server.Transfer("ShowMsg.aspx?msgtype=nompi&msg=没有相关信息");
                    b = false;
                }  
            }
            return b;

        }
       
        private void AJAX_Action()
        {
            string result = string.Empty;
            try


            {
                if (Request.Cookies["testCookie"] == null && Request.QueryString["GH"] != null)
                {
                    result = "{status:-1,message:'您的浏览器设置已被禁用 Cookies,您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。'}";
                }
                else if (Request.QueryString["GH"] != null && Request["Pwd"] == null)
                {
                    result = GetYongHuXX(Request.QueryString["GH"]);
                }    
                else if (Request.QueryString["GH"] != null && Request.QueryString["Pwd"] != null)
                {
                    result = CheckedPassword(Request.QueryString["GH"], Request.QueryString["Pwd"]);
                }              
            }
            catch (Exception ex)
            {
                Logger.Write(string.Format("Soruce:{0}\r\nMessage:{1}\r\nTrace:{2}\r\nDateTime:{3}",
                    ex.Source, ex.Message, ex.StackTrace, DateTime.Now),
                    "Exception Log");
                result = "{status:-1,message:'登录失败(" + ex.Message.Replace("\r", "").Replace("\n", "") + "),请重试'}";
              
            }
            if (result.Length > 0)
            {
                Response.Clear();
                Response.Write(result);
                Response.End();
            }
        }
        /// <summary>


        /// 根据工号得到用户信息
        /// </summary>
        /// <param name="gh">工号</param>
        /// <returns></returns>
        private string GetYongHuXX(string gh)
        {
            DataSet ds = CommanBll.Get_EHR_YONGHULOGIN("And  SHENFENZH='" + gh.Trim() + "' and ROWNUM=1");
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                CookieInfo.SHENFENZH = ds.Tables[0].Rows[0]["SHENFENZH"].ToString();
                CookieInfo.XINGMING = ds.Tables[0].Rows[0]["XINGMING"].ToString();
                CookieInfo.MPI = ds.Tables[0].Rows[0]["MPI"].ToString();
                CookieInfo.XINGBIE = ds.Tables[0].Rows[0]["XINGBIEMC"].ToString();
                CookieInfo.DYZ_USERID = ds.Tables[0].Rows[0]["SHENFENZH"].ToString();
                CookieInfo.DYZ_USERNAME = ds.Tables[0].Rows[0]["XINGMING"].ToString();
                CookieInfo.CHUSHENGRQ = ds.Tables[0].Rows[0]["CHUSHENGRQ"].ToString();
                return "{status:0,message:'" + ds.Tables[0].Rows[0]["XINGMING"].ToString() + "'}";
            }
            else
            {
                return "{status:-1,message:'身份证号不存在或此身份证号已被停用!'}";
            }
        }
        private string GetYongHuXX2(string gh)
        {          
            IList<Entity.EHR_MPI> entityList = Bll.GetEntityList("SHENFENZH='" + gh.Trim() + "'", "");

            if (entityList != null&& entityList.Count>0)
            {
                Entity.EHR_MPI entyh = entityList[0];
                CookieInfo.SHENFENZH = entyh.SHENFENZH;
                CookieInfo.XINGMING = entyh.XINGMING;


                CookieInfo.MPI = entyh.MPI;
                CookieInfo.XINGBIE = entyh.XINGBIEMC;
                CookieInfo.DYZ_USERID = entyh.SHENFENZH;
                CookieInfo.DYZ_USERNAME = entyh.XINGMING;              
                FormsAuthentication.SetAuthCookie(CookieInfo.MPI, false);
            }
            return "0";
        }
}  
   


[解决办法]
看不懂你要提什么问题。

热点排行