Nasıl bu iki işlev Ajax ile çalışmak ve nasıl jQuery bunu yeniden almak mı?

1 Cevap php
function ajaxFunction(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 itibaren 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); 

}

Bu fonksiyon harika çalışıyor ve hiçbir sorunları var. Ama eklerseniz:

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 itibaren 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); 

}

Ben yürütmeye çalıştığınızda ajaxFunction('returnAllProducts') alıyorum:

    syntax error
 });\n

itibaren

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

    }); <<<----

ve

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

Yani benim sorular şunlardır:

a) how do I convert this over to jquery? I've read over some jquery ajax tutorials, but I wasn't able to make the connection how to do what I am doing here. b) How do I get both functions to work? I know the PHP behind them works fine, but I can't even test if refreshProduct() works properly right now.

1 Cevap

Sen eksik gibi görünüyor bir}

Bu düzgün girintili, kodunuz ...

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

Olması gerektiği zaman

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

Ayrıca, bundan sonra iki} bu gibi kod render, silinmesi gerekir:

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 kullanarak bu yeniden gelince, gerçekten çok kolay:

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);
    });
}