标题:一个Ajxa问题
只看楼主
liangwenbc
Rank: 3Rank: 3
来 自:广州
等 级:论坛游侠
帖 子:168
专家分:174
注 册:2008-6-11
结帖率:75%
已结贴  问题点数:20 回复次数:2 
一个Ajxa问题
function Ajax() {
    var m_xmlReq = false;

    if (window.ActiveXObject) {                     //判断是否IE浏览器并创建异步对象
        try {
            m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e) {
            try {
                m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e) { }
        }
    }
    else if (window.XMLHttpRequest) {
        m_xmlReq = new XMLHttpRequest();
    }

    this.send = function(Url, send_data, CallBack) {
        if (!m_xmlReq) {
            return;
        }
        method = "POST";
        m_xmlReq.open(method, Url, true);           //加载服务器
        if (method == 'POST') {
            m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
        }
        m_xmlReq.onreadystatechange = function() {  //关联异步调用方法
            if (m_xmlReq.readyState == 4) {         //判断异步调用完成
                if (m_xmlReq.status == 200) {       //判断是否调用成功                    
                    CallBack(true, m_xmlReq.responseText);
                } else {
                    CallBack(false);
                }
            }
        }
        m_xmlReq.send(send_data.toString());        //发送带参数的请求
    }
}
var AjaxObject = new Ajax();
function toLogin() {
    var username = document.getElementById("txtName").value;
    var pwd = document.getElementById("txtPwd").value;
    AjaxObject.send("login.aspx", "username=" + username + "&pwd=" + pwd,
            function(succeed, responseUrl) {        //绑定回调函数
                if (succeed) {
                    var arr = responseUrl.split("|");
                    if (arr[0] == 'true') {
                        document.URL = arr[1];      //设置登录成功后跳转的URL
                    }
                    else {
                        alert("用户名或者密码错误");
                    }
                }
            }
        );
}
请问大家this.send = function(Url, send_data, CallBack)这句是什么意思,是哪部分的知识,为何我找不到相关的解释。比如 function(Url, send_data, CallBack)这句,我只查到一个“$(selector).load(url,data,callback)”,来之:http://www.
还有 CallBack(true, m_xmlReq.responseText);这句。
以上问题麻烦大家给我解释下,有相关知识链接也可以。不胜感激

搜索更多相关主题的帖子: function IE浏览器 false 
2012-02-03 18:53
yms123
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
得分:10 
function Ajax()
{
    var m_xmlReq = false;

    if (window.ActiveXObject)  //判断是否IE浏览器并创建异步对象
    {                    
        try
        {
            m_xmlReq = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e)
        {
            try
            {
                m_xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e) { }
        }
    }
    else if (window.XMLHttpRequest)
   {
        m_xmlReq = new XMLHttpRequest();
    }

    this.send = function(Url, send_data, CallBack)
    {
        if (!m_xmlReq)
        {
            return;
        }
        method = "POST";
        m_xmlReq.open(method, Url, true);           //加载服务器
        if (method == 'POST')
        {
            m_xmlReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
        }
        m_xmlReq.onreadystatechange = function()//关联异步调用方法
        {  
            if (m_xmlReq.readyState == 4)
            {         //判断异步调用完成
                if (m_xmlReq.status == 200)
                {       //判断是否调用成功                    
                    CallBack(true, m_xmlReq.responseText);
                }
               else
               {
                    CallBack(false);
                }
            }
        }
        m_xmlReq.send(send_data.toString());        //发送带参数的请求
    }
}
var AjaxObject = new Ajax();
function toLogin() {
    var username = document.getElementById("txtName").value;
    var pwd = document.getElementById("txtPwd").value;
    AjaxObject.send("login.aspx", "username=" + username + "&pwd=" + pwd,
            function(succeed, responseUrl)
            {        //绑定回调函数
                if (succeed)
                {
                    var arr = responseUrl.split("|");
                    if (arr[0] == 'true')
                    {
                        document.URL = arr[1];      //设置登录成功后跳转的URL
                    }
                    else
                    {
                        alert("用户名或者密码错误");
                    }
                }
            }
        );
这段代码里使用了两个知识点
this.send = function(Url, send_data, CallBack)
这里是javascript面向对象编程的知识点,lz可以去看javascript实现面向对象编程
CallBack这个叫做函数参数,也就是把一个函数作为另一个函数的参数来传递,这就是函数参数
 AjaxObject.send("login.aspx", "username=" + username + "&pwd=" + pwd,
            function(succeed, responseUrl) {        //绑定回调函数
                                                    //这里这个绑定回调函数用了一种匿名函数的写法
                                                   //通常来说声明函数必须写函数名,而这种写法可以省略函数名
                                                   //不过匿名函数会立即执行并且,匿名函数由于没用名称无法进行再次调用
                                                   //虽然从程序上这么写可以也不会出错误,但是从规范化角度来说不推荐,因为一旦函数内部出错不好调试。
                if (succeed) {
                    var arr = responseUrl.split("|");
                    if (arr[0] == 'true') {
                        document.URL = arr[1];      //设置登录成功后跳转的URL
                    }
                    else {
                        alert("用户名或者密码错误");
                    }
                }
            }
        );
2012-02-04 11:47
dengyan
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2012-2-7
得分:10 
这个写法已经过时啦
2012-02-07 19:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-360358-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021669 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved