Ciao a tutti,
sto provando da giorni, senza risultati, a risolvere questo problema:
come si scarica un json da javascript? Non sono un mago di javascript, però ho una dimestichezza molto buona con il C. Purtroppo non riesco a concludere nulla: il codice che ho messo insieme fino ad ora è questo
<script type="text/javascript" >
//Crea socket richiesta ajax
function CreateXmlHttpReq2(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;
}
//scatta quando arrivano i dati richiesti da quella pagina
function myHandler2() {
document.write("Stato: "+myRequest.status+" ReadyState="+myRequest.readyState);
if (myRequest.readyState == 4 /*&& myRequest.status == 200*/)
{
Load(eval(myRequest.responseText));
}
}
//funzione da chiamare quando vuoi che venga interrogata la pagina all'url
function Interroga(url)
{
myRequest = CreateXmlHttpReq2(myHandler2);
myRequest.open("GET",url,true);
myRequest.send(null);
}
//QUI USI IL VALORE CHE TI HA RITORNATO LA PAGINA
function Load(obj)
{
document.write("FATTO!
"+obj);
}
var myRequest = null;
Interroga(unurlremoto.php);
</script>
I problemi sono diversi
1) su firefox myRequest.readyState rimane bloccato a 2, e non ne comprendo il motivo ( forse qualche impostazione del browser? )
2) Su chrome funziona.. ma perché myRequest.status rimane a Zero invece che diventare 200? ( il file che scarico è su un RUL remoto )
3) Provo a stampare quanto ritornato dal json che ( forse ) è stato scaricato, e ottengo la scritta undefined. Il punto è questo: ho effettivamente scaricato quello che mi serve?
Avessi la certezza di avere in mano almeno una stringa potrei creare io stesso il parsing adeguato, ma avendo problemi sia con lo scaricare i dati sia con il json ( non ho esperienza su questi temi ) ho bisogno di un piccolo aiuto. Resto a disposizione per chiarimenti, grazie mille