实名认证用户熊川湘 身份证号码430811198506290914

xmlhttp 的具体应用

IE 的 xmlhttp是activeX , Firefox的xmlhttp支持是内建的。可以参看AJAX入门书籍,如何建立XMLHttpRequest。

var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

 

部分样例代码:

var xmlHttp = false;

function add2cart(prodkey)
{
    if (window.XMLHttpRequest) { 
    xmlHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = "http://www.cnblogs.com/func/orderproc/add2cart.aspx?prodkey=" + prodkey;
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = showMsg;
    xmlHttp.send(null);
}

function showMsg()
{
    if (xmlHttp.readyState == 4)
    {
        var response = xmlHttp.responseText;
        alert(response);
        if (response == "1")
            alert("成功加入到购物车");
        else if (response == "0")
            alert("该产品已经在购物车中");
        else
            alert("添加失败,请重试或联络在线客服");
        if (response == "1" || response == "0")
        {
            var objs = document.getElementsByName("linkcheckout");
            if (objs[0].style.display=="none")
            {
                for (var i=0; i<objs.length; i++)
                    objs[i].style.display = "";
            }
        }
    }
}

posted @ 2010-08-24 17:29  浪达短信群发  阅读(170)  评论(0编辑  收藏  举报