Visualizzazione dei risultati da 1 a 8 su 8

Discussione: gestire json con PHP

  1. #1

    gestire json con PHP

    codice:
    function doJSON() {
        var user = getUserObject();
        
        var userAsJSON = JSON.stringify(user);
        alert("ecco l'oggetto User di Json :\n " + userAsJSON);
        
        var url = "json_first.php?timeStamp=" + new Date().getTime();
        
        createXMLHttpRequest();
        xmlHttp.open("POST", url, true);
        xmlHttp.onreadystatechange = handleStateChange;
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
        xmlHttp.send(userAsJSON);
    }
    Mando con questa funzione dei dati a una pagina php, come faccio a gestirli da PHP?

    Se mando una stringa di Post così come la riuso dalla pagina PHP?
    {"username":"bonzo","mail":"ilbonzo.org@gmail.com" }

    Nell'esempio che ho trovato usa una servlet Java, io vorrei farlo con PHP.

  2. #2
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  3. #3
    Ok, ma non capisco che variabile ci metto dentro json_decode?

    in che variabile è il valore della stringa che ho inviato?

  4. #4
    Semplificato :
    Codice PHP:
    var data='{"username":"bonzo","mail":"ilbonzo.org@gmail.com"}';
    var 
    postVar'user_data='+encodeURIComponent(data.toJSONString());
    xhr.send(postVar); 
    in php
    Codice PHP:
    if(get_magic_quotes_gpc()){
        
    $jsonStringstripslashes($_POST['user_data']);
    }
    var_dump(json_decode($jsonString)); 


    PS.

    Dai un occhio alla mia firma
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  5. #5
    Così mi sembra di capirlo, ma nell'esempio con le servlet lo manda via post senza mandarlo sotto forma di querystring.

    E' questo che non capisco?
    Forse è un problema del cavolo, ma volevo capire come mai.

  6. #6
    Così mi sembra di capirlo, ma nell'esempio con le servlet lo manda via post senza mandarlo sotto forma di querystring.


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  7. #7
    non cambia assolutamente nulla ... se viene passato via post lo devi leggere via post

    Questo viene fatto perché "tecnicamente" la lunghezza di una richiesta (ovvero PATH + QUERYSTRING) non dovrebbe superare i 255 caratteri, ma in realtà nessun browser ha questo tipo di problemi
    The fastest Redis alternative ... cachegrand! https://github.com/danielealbano/cachegrand

  8. #8

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.