2010-02-08 3 views
0

fonction ajaxFunction (phpFunction) { var ajaxRequête; // La variable qui rend l'Ajax possible!Comment puis-je faire fonctionner ces deux fonctions avec Ajax, et comment le réécrire dans Jquery?

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

} 

Cette fonction fonctionne très bien et n'a aucun problème. Mais si j'ajoute:

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

} 

Lorsque je tente d'exécuter ajaxFunction('returnAllProducts') je reçois:

syntax error 
});\n 

de

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

    }); <<<---- 

et

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

Mes questions sont les suivantes:

a) comment puis-je convertir ceci en jquery? J'ai lu quelques tutoriels jquery ajax, mais je n'ai pas été en mesure de faire la connexion comment faire ce que je fais ici. b) Comment puis-je faire fonctionner les deux fonctions? Je sais que le PHP derrière eux fonctionne très bien, mais je ne peux même pas tester si refreshProduct() fonctionne correctement maintenant.

Répondre

1

Vous semblez manquer un}

ceci est votre code, bien échancré ...

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

Quand il doit être

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

En outre, les deux} après devrait être supprimé, rendant votre code comme ceci:

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

En ce qui concerne la réécriture en utilisant ce jQuery, il est vraiment facile:

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

vient de modifier ma réponse, check it out. – Johnco

+0

Gah! J'aime la réécriture, mais s'il vous plaît utiliser un stockage de sélecteur et de chaînage! Comme '$ ('.' + IdNum) .empty(). Html (data);' –

+0

En fait, $ .get n'est pas vraiment équivalent car il ne fournit pas de gestion des erreurs, vous devrez utiliser $ .ajax –

Questions connexes