繁体   English   中英

如何使这两个函数与Ajax一起使用,以及如何在Jquery中重写它?

[英]How do I get these two functions to work with Ajax, and how to I rewrite it in Jquery?

函数ajaxFunction(phpFunction){var ajaxRequest; //使Ajax成为可能的变量!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
    $('.subCat').html(ajaxRequest.responseText);
    $('.subCat').ready(function(){
    $('.subCat').fadeIn();
    });





        }
    }


    var url = "products.php?func=" + phpFunction;

    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null); 

}

此功能很好用,没有问题。 但是,如果我添加:

function refreshProduct(idNum, phpFunction){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
        $('.' + idNum).empty();
    $('.' + idNum).html(ajaxRequest.responseText);

    });





        }
    }


    var url = "products.php?func=" + phpFunction + "&idNum=" + idNum;

    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null); 

}

当我尝试执行ajaxFunction('returnAllProducts')我得到:

    syntax error
 });\n

   $('.' + idNum).html(ajaxRequest.responseText);

    }); <<<----

ajaxFunction is not defined
javascript:ajaxFunction('returnAllProducts')()

所以我的问题是:

a)如何将其转换为jquery? 我已经阅读了一些jquery ajax教程,但是无法建立连接如何执行我在这里所做的事情。 b)如何使两个功能同时起作用? 我知道它们后面的PHP可以正常工作,但是我什至无法测试refreshProduct()现在是否正常工作。

您似乎缺少}

这是您的代码,已正确缩进...

// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        $('.'idNum).empty();
        $('.'idNum).html(ajaxRequest.responseText);
    });

什么时候应该

// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        $('.'idNum).empty();
        $('.'idNum).html(ajaxRequest.responseText);
    }
});

另外,应删除此后的两个},以使代码如下所示:

function refreshProduct(idNum, phpFunction){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            $('.' + idNum).empty();
            $('.' + idNum).html(ajaxRequest.responseText);
        }
    });

    var url = "products.php?func=" + phpFunction + "&idNum=" + idNum;

    ajaxRequest.open("GET", url, true);
    ajaxRequest.send(null); 
}

至于使用jQuery重写它,这确实很容易:

function ajaxFunction(phpFunction){
    var url = "products.php?func=" + phpFunction;
    $.get(url, function(data) {
        $('.subCat').html(data).ready(function(){
            $('.subCat').fadeIn();
        });
    });
}

function refreshProduct(idNum, phpFunction){
    var url = "products.php?func=" + phpFunction + "&idNum=" + idNum;
    $.get(url, function(data) {
        $('.' + idNum).empty().html(data);
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM