Ajax 手写JS

var httpRequest = false;
var mesdivs = null
function sendRequest(url, mesdiv) {
 
    mesdivs = mesdiv;
    httpRequest = false;
    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {

        }
    }
    else if (window.ActiveXObject) {

        try {

            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");

        }
        catch (e) {

            try {

                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

            }
            catch (e) {
            }
        }
    }

    if (!httpRequest) {

        window.alert("no");
    }

    if (mesdivs == "sendmes") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    alert(string);

                    window.parent.document.location.reload();

                }
            }
        }
    }
    if (mesdivs == "addit") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    alert(string);

                    location.reload();

                }
            }
        }
    }
    if (mesdivs == "addclick") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                }
            }
        }
    }
    if (mesdivs == "confirm") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    alert(string);

                    location.reload();

                }
            }
        }
    }
    if (mesdivs == "AddReply") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {               
                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    sendRequest("/DataAccess/getComments.aspx?id=" + parent.document.getElementById("Hidden1").value, "getComments");
                    alert(string);
                    SetWinHeight(document.getElementById("win"));
                }
            }
        }
    }
  
      if (mesdivs == "addFTopic") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                    if (string == "0") {
                        alert("提交内容成功!");
                        location.href = "topic-father.aspx";
                    }
                    else if (string == "1") {
                        alert("提交内容失败");
                    }
                    else {
                        alert("提交内容失败");
                    }
                }
            }
        }
    }
    if (mesdivs == "addMTopic") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;
                    if (string == "0") {
                        alert("提交内容成功!");
                        location.href = "topic-mother.aspx";
                    }
                    else if (string == "1") {
                        alert("提交内容失败");
                    }
                    else {
                        alert("提交内容失败");
                    }
                }
            }
        }
    }
    if (mesdivs == "addWTopic") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                    if (string == "0") {
                        alert("提交内容成功!");
                        location.href = "topic_wedding.aspx";
                    }
                    else if (string == "1") {
                        alert("提交内容失败");
                    }
                    else {
                        alert("提交内容失败");
                    }
                }
            }
        }
    }
    if (mesdivs == "getComments") {
        httpRequest.onreadystatechange = function() {       
            if (httpRequest.readyState == 4) {               
                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    document.getElementById("DivContent").innerHTML = string;


                }
            }
        }
    }
    if (mesdivs == "getComments2") {
        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    document.getElementById("DivContent").innerHTML = string;


                }
            }
        }
    }

    if (mesdivs == "viewCart2") {
        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {
                alert(ttpRequest.status);
                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    document.getElementById("divs").innerHTML = string;


                }
            }
        }
    }
    if (mesdivs == "getCareobject") {
        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    document.getElementById("info1").innerHTML = string;


                }
            }
        }
    }
    if (mesdivs == "getReminder") {
        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    document.getElementById("info2").innerHTML = string;


                }
            }
        }
    }
    if (mesdivs == "getLogin") {
        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {
                    alert(url);
                    var string = httpRequest.responseText;
                    alert(string);
                    document.getElementById("divproInfo").innerHTML = string;


                }
            }
        }
    }
    if (mesdivs == "addProCookie") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {              
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                }
            }
        }
    }
    if (mesdivs == "getCollData") {
        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                    if (string == "0") {
                        alert("您还没有登录!");
                        parent.hideWinLay();
                    }
                    else if (string == "1") {
                        alert("收藏成功!");
                        parent.hideWinLay();
                    }
                    else if (string == "2") {
                        alert("收藏失败!");
                        parent.hideWinLay();
                    }
                    else if (string == "99") {
                        alert("收藏失败!");
                        parent.hideWinLay();
                    }
                    else {
                        alert("您已经收藏该礼品!");
                        parent.hideWinLay();
                    }
                }
            }
        }
    }
    if (mesdivs == "getWishData") {
        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                    if (string == "0") {
                        alert("您还没有登录!");
                        parent.hideWinLay();
                    }
                    else if (string == "1") {
                        alert("添加愿望成功!");
                        parent.hideWinLay();
                    }
                    else if (string == "2") {
                        alert("添加愿望失败!");
                        parent.hideWinLay();
                    }
                    else if (string == "99") {
                        alert("添加愿望失败!");
                        parent.hideWinLay();
                    }
                    else {
                        alert("您已经将该礼品添加到我的愿望!");
                        parent.hideWinLay();
                    }
                }
            }
        }
    }
    if (mesdivs == "getproInfo") {

        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;                   
                    document.getElementById("tab_Recom").innerHTML = string;
                }
            }
        }
    }
    if (mesdivs == "getHotPro") {

        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;                  
                    document.getElementById("divhotpro").innerHTML = string;
                }
            }
        }
    }
  
    if (mesdivs == "BindMobile") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;

                    document.getElementById("HiddenField1").value = string;
                    St();


                }
            }
        }
    }
    if (mesdivs == "sendTel") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;
                    if (string == "0") {
                        alert("您的用户名或手机错误!");
                    }
                    else if (string == "1") {
                        location.href = "getpass_2_mobile.aspx";
                    }
                    else if (string == "3") {
                        alert("验证码错误!");
                    }
                    else {
                        alert("发送失败,请联系客服!");
                    }
                }
            }
        }
    }
    if (mesdivs == "sendPayTel") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                    if (string == "0") {
                        location.href = "getPaypass_3.aspx";
                    }
                    else if (string == "1") {
                        location.href = "../reg/wrong.htm";
                    }
                    else {
                        location.href = "www.guanxin.com";
                    }
                }
            }
        }
    }
    if (mesdivs == "sendEmail") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;
                    if (string == "0") {
                        alert("您的邮箱或用户名错误!");
                    }
                    else if (string == "1") {
                        location.href = "getpass_2_email.aspx";
                    }
                    else if (string == "3") {
                        alert("验证码错误!");
                    }
                    else {
                        alert("发送失败,请联系客服!");
                    }
                }
            }
        }
    }
    if (mesdivs == "sendPayEmail") {

        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {

                if (httpRequest.status == 200) {

                    var string = httpRequest.responseText;
                    if (string == "0") {
                        location.href = "getPaypass_3.aspx";
                    }
                    else if (string == "1") {
                        location.href = "../reg/wrong.htm";
                    }
                    else {
                        location.href = "www.guanxin.com";
                    }
                }
            }
        }
    }
    if (mesdivs == "handledTel") {
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                    if (string != "0") {
                        location.href = "getpass_3.aspx?name=" + string;
                    }
                    else {
                        alert("error");
                    }
                }
            }
        }
    }
    if (mesdivs == "handledEmail") {
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                    if (string != "0") {
                        location.href = "getpass_3.aspx?name" + string;
                    }
                    else {
                        alert("error");
                    }
                }
            }
        }
    }
    if (mesdivs == "handledPay") {
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    var string = httpRequest.responseText;
                    if (string == "0") {
                        location.href = "../reg/right.htm";
                    }
                    else {
                        location.href = "../reg/wrong.htm";
                    }
                }
            }
        }
    } 
    else if (mesdivs == "th") {
        httpRequest.onreadystatechange = function() {

            if (httpRequest.readyState == 4) {
                if (httpRequest.status == 200) {
                    $("ListBox3").options.length = 0;

                    var string = httpRequest.responseText;
                    var str = new Array();
                    str = string.split("|");
                    for (d = 0; d < str.length; d++) {
                        var se = new Array();
                        se = str[d].split(",");

                        jsAddItemToSelect($("ListBox3"), se[1], se[0]);
                    }
                }
            }
        }

 

    }
    httpRequest.open('get', url, true);
    httpRequest.send(null);
}


function GetFirst(a) {

    if (a == "se") {
        //$("daZhongXiao").innerHTML=$("ListBox1").item.innerText;
        $("HiddenField1").value = $("ListBox1").value;
        sendRequest("/dataAccess/getsecondtype.aspx?name=" + $("ListBox1").value + "&type=" + a, a);
    }
    else if (a == "th") {//$("daZhongXiao").innerHTML=$("daZhongXiao").innerHTML+">>"+$("ListBox2").value;
        $("HiddenField2").value = $("ListBox2").value;
        sendRequest("/dataAccess/getsecondtype.aspx?name=" + $("ListBox2").value + "&type=" + a, a);
    }
    else {//$("daZhongXiao").innerHTML=$("daZhongXiao").innerHTML+">>"+$("ListBox3").value;
        $("HiddenField3").value = $("ListBox3").value;
    }
}
function $(a) {
    return document.getElementById(a);
}
function jsSelectIsExitItem(objSelect, objItemValue) {
    var isExit = false;
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].value == objItemValue) {
            isExit = true;
            break;
        }
    }
    return isExit;
}

function jsAddItemToSelect(objSelect, objItemText, objItemValue) {

    if (jsSelectIsExitItem(objSelect, objItemValue)) {

    }
    else {
        var varItem = new Option(objItemText, objItemValue);
        //      objSelect.options[objSelect.options.length] = varItem;
        objSelect.options.add(varItem);

    }
}

function jsRemoveItemFromSelect(objSelect, objItemValue) {
    if (jsSelectIsExitItem(objSelect, objItemValue)) {
        for (var i = 0; i < objSelect.options.length; i++) {
            if (objSelect.options[i].value == objItemValue) {
                objSelect.options.remove(i);
                break;
            }
        }

    }
    else {
    }
}

function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {
    if (jsSelectIsExitItem(objSelect, objItemValue)) {
        for (var i = 0; i < objSelect.options.length; i++) {
            if (objSelect.options[i].value == objItemValue) {
                objSelect.options[i].text = objItemText;
                break;
            }
        }
    }
    else {
    }
}
function jsSelectItemByValue(objSelect, objItemText) {
    var isExit = false;
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].text == objItemText) {
            objSelect.options[i].selected = true;
            isExit = true;
            break;
        }
    }
    if (isExit) {
    }
    else
    { }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以手写一个简单的$.ajax()请求,步骤如下: 1. 首先,需要创建一个XMLHttpRequest对象。您可以使用下面的代码来创建一个兼容不同浏览器的XMLHttpRequest对象: ```javascript function getXhr() { var xhr = null; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { xhr = new ActiveXObject('Microsoft.XMLHTTP'); } return xhr; } ``` 这个函数会返回一个XMLHttpRequest对象。 2. 接下来,您可以使用上述函数获取到XMLHttpRequest对象,并使用它来发送请求。下面是一个简单的示例代码: ```javascript var xhr = getXhr(); // 获取XMLHttpRequest对象 xhr.open('GET', 'your-url'); // 设置请求方法和URL xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { // 处理响应数据 var response = xhr.responseText; console.log(response); } }; xhr.send(); // 发送请求 ``` 在上述代码中,您需要将'your-url'替换为您要发送请求的URL。然后,您可以在xhr.onreadystatechange事件处理程序中处理响应数据。在这个示例中,我们只是简单地将响应数据打印到控制台。 这样,您就可以手写一个简单的$.ajax()请求了。这种方式相比于使用jQuery的$.ajax()方法,可以减少对整个jQuery库的依赖,并且可以更加灵活地控制请求过程。同时,您可以根据需要自定义请求头、请求方法等。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值