Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2004
    Messaggi
    263

    Trasferimento dati in Json

    Ciao a tutti.
    Premetto che non sono un esperto di JS.
    Per un mio cliente devo esportare dei dati in formato JSON da un sito e leggerli con un script.
    Il rete ho trovato il seguente esempio che mi permetterebbe di fare qualcosa di analogo:
    Lato client:
    codice HTML:
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <h2>Test</h2>
    <script>
    function getData() {
      var the_object ;
      var xhr = new XMLHttpRequest() ;
      var url = "http://www.miosito.com/test.js" ;
      xhr.open("GET", url, true) ;
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
        console.log("State ready!") ;
        if (xhr.status == 200) {
          the_object = JSON.parse(xhr.responseText) ;
          msg  = the_object.items[0].value + "-" ;
          msg += the_object.items[0].order ;
          alert(msg) ;
        } else {
          alert("Si e' verificato un problema con l'URL.");
        }
        }
      } ;
      console.log("Sending request") ;
      xhr.send(null) ;
    }
    </script>
    </body>
    </html>
    Lato server (cioè il file test.js, in formato json):
    codice HTML:
    { "type": "Sample",
      "value": "List",
      "items": [
        {"value": "Uno", "order": "Primo"  },
        {"value": "Due", "order": "Secondo"},
        {"value": "Tre", "order": "Terzo"  }
      ] }
    Ma a me non funziona. Praticamente a schermo non da errori ma non visualizza nulla.
    Dove sbaglio ?

    Grazie 1000

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,134
    La console da errori?
    Vedi http://www.json.org/
    Mi sembra (ma non sono preparato in merito) che sia un json bidimensionale e credo vada letto diversamente.
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

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