Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 11

Discussione: jQuery 1.5 e ajax/json

  1. #1

    jQuery 1.5 e ajax/json

    Salve avrei bisogno del vostro aiuto per poter usare jquery 1.5 per le chiamate ajax.. attualmente uso questo codice :

    Per il parsing dei dati che ricevo in json :

    https://github.com/douglascrockford/...aster/json2.js

    e nel mio codice ho :

    codice:
    function CreazioneAJAX(){
     var activexmodes=["Msxml2.XMLHTTP", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP.6.0" ,"Microsoft.XMLHTTP"] //Per IE
     if (window.ActiveXObject){ //Test per il supporto per ActiveXObject in IE  (L' XMLHttpRequest in IE7 è errato)
      for (var i=0; i<activexmodes.length; i++){
       try{
        return new ActiveXObject(activexmodes[i])
       }
       catch(e){
        //rimuove errori
       }
      }
     }
     else if (window.XMLHttpRequest) // Per Mozilla, Safari etc
      return new XMLHttpRequest()
     else
      return false
    }
    
      var post = "command=status&subsystem=main&dummy=" + (new Date()).getTime();
      
      var req = CreazioneAJAX();
      req.open("POST", URL + "/ajax", false);
      req.send(post);
      var json = JSON.parse(req.responseText);
    Come posso tradurre in codice jquery tutto questo usando solo la libreria jquery? Potrei usare $.ajax o $.post? se si come?

    Grazie a chi vorrà delucidarmi sulla questione...
    In God We Trust

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    codice:
    $.ajax({
       type: "POST",
       dataType: "json",
       url: url,
       success: function(msg){
         //codice
       }
     });

  3. #3
    Grazie quindi nel mio caso dovrei usare un codice del genere :

    Prima :

    codice:
    var post = "command=status&subsystem=main&dummy=" + (new Date()).getTime();
      
      var req = CreazioneAJAX();
      req.open("POST", URL + "/ajax", false);
      req.send(post);
      var json = JSON.parse(req.responseText);

    ORA :

    codice:
     $.ajax({
       type: "POST",
       dataType: "json",
       url: URL + "/ajax",
       data: post,
       async:false,
       success: function(msg){
    var json = msg;
    
    var infotitle = json.version;
    }
     });
    oppure

    codice:
     var json = $.ajax({
       type: "POST",
       dataType: "json",
       url: URL + "/ajax",
       data: post,
       async:false,
     }).responseText;
    
    var infotitle = json.version;
    Ciao
    In God We Trust

  4. #4
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    codice:
     $.ajax({
       type: "POST",
       dataType: "json",
       url: URL + "/ajax",
       //data: post,           qua ci devi mettere i parametri da passare in post
       success: function(json){
    var infotitle = json.version;
    }
     });

  5. #5
    Originariamente inviato da Vindav
    codice:
    ...
       //data: post,           qua ci devi mettere i parametri da passare in post
    ma la mia variabile post è :

    codice:
    var post = "command=status&subsystem=main&dummy=" + (new Date()).getTime();
    non va bene cosi?

    comunque ho provato cosi :

    codice:
    $.ajax({
       type: "POST",
       dataType: "json",
       url: URL + "/ajax",
       data: "command=status&subsystem=main&dummy=" + (new Date()).getTime(),
       success: function(json){
    var infotitle = json.version;
    alert(infotitle);
    }
     });
    ma l'alert non viene eseguito e nemmeno la json.version viene settata..

    usando invece la classica chiamata ajax come scritto nel primo post che poi faccio parsare in json :

    codice:
     var post = "command=status&subsystem=main&dummy=" + (new Date()).getTime();
    
      var req = createAJAX();
      req.open("POST", URL + "/ajax", false);
      req.send(post);
      var json = JSON.parse(req.responseText);
    
    /* la req.responseText di ritorno è uguale a :
    
     { "version" : "11223344", "subversion" : "4455"}
    
     quindi json.version = 11223344 */
    ciao
    In God We Trust

  6. #6
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    verifica se ti da errori sulla console di firefox, l'ultimo che hai postato mi pare corretto...

  7. #7
    nessun errore nella console Firefox comunque ho aggiunto il parametro error:

    codice:
    $.ajax({
       type: "POST",
       dataType: "json",
       url: URL + "/ajax",
       data: "command=status&subsystem=main&dummy=" + (new Date()).getTime(),
       success: function(json){
    var infotitle = json.version;
    alert("pippo");
    },
    error:function (stato, ajaxOptions, errore){
                        alert(stato.status + " - " + errore);
                    }  
     });
    in questo caso ho error:

    0 - No Transport

    Ciao
    In God We Trust

  8. #8
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    No transport indica un qualche tipo di errore nella comunicazione tra client e server... che versione usi di jquery? installa firebug, puoi vedere se la chiamata è andata a buon fine.

    p.s. l'url che invochi sta nello stesso dominio della pagina che sta effettuando la chiamata?

  9. #9
    Uso Jquery 1.5 come da titolo...

    sto testando in locale con xamp e con ie in actionscript e la var URL nel mio caso corrisponde a "http://192.168.1.36:16002"

    che con il vecchio procedimento ajax funge..

    Ho già firebug 1.6.2 ma non mi segnala errori di sorta.. solo nel pannello net mi da una segnalazione 404 not fund sulla post ajax per localhost.. forse è quiil problema localhost invece che 192.168.1.36..?..

    Ciao
    In God We Trust

  10. #10
    Utente di HTML.it
    Registrato dal
    Dec 2010
    Messaggi
    3,660
    si credo sia quello il problema, il codice per la chiamata ajax è corretto... verifica che l'url sia quello atteso e come mai vedi localhost invece dell'ip su firebug, piu di cosi credo sia impossibile aiutarti sono impostazioni della macchina su cui stai lavorando

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.