Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it L'avatar di strae
    Registrato dal
    Apr 2008
    Messaggi
    407

    Recuperare oggetto JSON da richiesta AJAX.. come?

    Ciao ragazzi, ho un problema..
    Praticamente, ho una pagina php che effettua una chiamata AJAX a un'altra pagina php, che a sua volta restituisce un oggetto JSON.. e fin qui tutto ok, il problema è che poi quella richiesta JSON viene vista come testo semplice (giustamente), e non come Json.

    Il codice che stò usando per la prova è:

    Codice PHP:
    //codice js che effettua la richiesta:
    var x = 0;
    var myRequest = new Array();
    function CreateXmlHttpReq(handler) {
        var xmlhttp = null;
        try {
            xmlhttp = new XMLHttpRequest();
        }catch(e){
            try{
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        xmlhttp.onreadystatechange = handler;
        return xmlhttp;
    }
    function prova(){
        x++;
        var r = Math.random();
        myRequest[x] = CreateXmlHttpReq(function() {myHH(x)});
        myRequest[x].open("GET","test-json-02.php?r=" + escape(r));
        myRequest[x].send(null);
    }
    function myHH(x) {
        if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
            mySo = myRequest[x].responseText;
            alert(mySo); //e qui si vede che la richiesta viene ricevuta correttamente
            var myJson = json_parse(mySo); //funzione json_parse: [url]http://www.JSON.org/json_parse.js[/url]
            //qui si blocca e non và più avanti!!
            
        }
    }


    //pagina php che restituisce il json:
    <?php
    header
    ('Content-Type: application/json');
    echo 
    '
    {
        "shipping":
            {
                "azienda": "nomeAzienda",
                "indirizzo": "via dei test n.1",
                "cap": "00000",
                "localita": "area di test",
                "provincia": "TS",
                "nazione": "Italia",
                "telefono": "0000-00000"
            },
        "company":
            {
                "codice": "000000",
                "azienda": "nome_azienda",
                "indirizzo": "via sdei test n.2",
                "cap": "00001",
                "localita": "Test area",
                "provincia": "TE",
                "piva": "0000000000000",
                "fiscale": "2222222222222",
                "telefono": "0000-000002",
                "fax": "5555-555555",
                "cellulare": "0000000000",
                "nome": "Test",
                "cognome": "Area"
            }
    }'
    ;
    Dov'è che sbaglio?
    Ho anche provato a usare jQuery, con la funzione getJSON:
    Codice PHP:
    function prova2(){
        $.
    getJSON("test-db-02.php", {"a""b"}, function(dati){
            
    alert('...');
            
    alert(dati);
        });

    Ma niente.
    Aiuti?
    You HAVE to assume your visitor is a maniac serial killer, out to destroy your application. And you have to prevent it.
    I can accept failure, everyone fails at something - But I can't accept not trying.

  2. #2
    meglio se usi questa:

    http://www.json.org/json2.js
    I DON'T Double Click!

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649

    Re: Recuperare oggetto JSON da richiesta AJAX.. come?

    e comunque
    alert(dati.shipping.azienda)
    restituirebbe "nomeAzienda"

    Originariamente inviato da strae
    Ho anche provato a usare jQuery, con la funzione getJSON:
    Codice PHP:
    function prova2(){
        $.
    getJSON("test-db-02.php", {"a""b"}, function(dati){
            
    alert('...');
            
    alert(dati);
        });

    Ma niente.
    Aiuti?

  4. #4
    Utente di HTML.it L'avatar di strae
    Registrato dal
    Apr 2008
    Messaggi
    407

    Re: Re: Recuperare oggetto JSON da richiesta AJAX.. come?

    Originariamente inviato da Xinod
    e comunque
    alert(dati.shipping.azienda)
    restituirebbe "nomeAzienda"

    ..si, ed è quello che volevo (logicamente l'alert è un test per vedere se l'array json è valido o meno)

    Cmq ho risolto, era il solito maledetto BOM byte
    You HAVE to assume your visitor is a maniac serial killer, out to destroy your application. And you have to prevent it.
    I can accept failure, everyone fails at something - But I can't accept not trying.

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 © 2025 vBulletin Solutions, Inc. All rights reserved.