ajax 请求ashx或asmx

24 篇文章 0 订阅

请求ashx

function GetJPData(domain, action, para, callfun) { $.getJSON(domain + "/Handler.ashx?action=" + action + "&" + para + "&fun=?", callfun); }

$("#btnLogin").click(function () {
    var userName = $("#userNameId").val();
    var userPwd = $("#userPwdId").val();
    var para = "userName=" + userName + "&userPwd=" + userPwd + "";
    GetJPData("", "", para, function (data) {
        alert(data.userState);
    })

 

  public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/javascript";
            context.Response.Write(Login());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        [WebMethod]
        public string Login()
        {
            string userName = HttpContext.Current.Request["userName"];
            string userPwd = HttpContext.Current.Request["userPwd"];
            string callfun = HttpContext.Current.Request["fun"];
            UserInfo userInfo = new UserInfo();
            string userState= userInfo.UserLogin(userName, userPwd);
            string userStr="'userState':"+userState;
            return callfun + "({" + userStr + "})";
        }

 

 

请求webservice

function JQAjax(url, vTime) {
    var m_Start = function () { isLoaded = false; };
    var m_Stop = function () { isLoaded = true; };
    var m_Error = function () { isLoaded = true; };
    var m_Success = function () { };
    var m_Timer = null;
    var m_Url = url;
    var overTime = 20000; //默认设置20秒超时
    if (vTime != null) { overTime = vTime; }
    this.OnStart = function (vPara) {
        m_Start = vPara;
    };
    this.OnStop = function (vPara) {
        m_Stop = vPara;
    };
    this.OnError = function (vPara) {
        m_Error = vPara;
    };
    this.OnSuccess = function (vPara) {
        m_Success = vPara;
    };
    this.OnTimeout = function (vPara) {
        m_Timer = window.setTimeout(vPara, overTime);
    };
    this.OnSend = function (sendData, DType, asyc) {
        $.ajax({
            global: false, //(默认: true) 是否触发全局 AJAX 事件
            async: asyc,  //异步请求。同步请求请设置为false同步请求将锁住浏览器。
            type: "POST", //默认为GET
            url: m_Url + "?rnd=" + GetRandomNum(1, 10000),
            cache: true,  //设置为false将不会从浏览器缓存中加载请求信息
            contentType: DType == "json" ? "application/json" : "application/x-www-form-urlencoded;charset=utf-8", //发送信息至服务器时内容编码类型
            data: sendData, //发送到服务器的数据
            dataType: DType, //xml,html,script,json,jsonp,text
            dataFilter: function (data, type) {
                return data // 返回处理后的数据
            },
            beforeSend: function (XMLHttpRequest) {
                try { m_Start(); } catch (e) { }
            },
            complete: function (XMLHttpRequest, textStatus) {
                if (m_Timer != null) { window.clearTimeout(m_Timer); }
                try { m_Stop(); } catch (e) { }
            }, //请求完成后回调函数 (请求成功或失败时均调用)
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //清除超时提示,并返回错误类型
                if (m_Timer != null) { window.clearTimeout(m_Timer); }
                try { m_Error(textStatus); } catch (e) { }
            },
            success: function (result) {
                //清除超时提示,并返回结果
                if (m_Timer != null) window.clearTimeout(m_Timer);
                try { m_Success(result); } catch (e) { }
            }
        });
    }
}

 

/*----产生范围内的随机数--------*/
function GetRandomNum(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
}

 

 var okAjax = new JQAjax("/WebService.asmx/InsertStuInfo");
        var _BuySendData = { 'stuNo': 0, 'stuName': 1,'stuScore':3 };
        okAjax.OnSuccess(function (result) {
            alert(result.d);
        }
            )

        //json请求方式须将参数构造成json字符串 xml直接传输json
       // var jsonText = JSON.stringify(_BuySendData);
       // alert(jsonText);

       //xml方式数据的解析   $(result).find("Code").text()

 

        okAjax.OnSend(_BuySendData, "json", true);

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ligengdipan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值