Search code examples
phpjqueryajaxgetstringify

Jquery ajax GET method with an object parse in JSON


I thank you to say your opinion about this misterious behaviour :

This code work :

JS code :

$.ajax({ 
        url: "ajouterEntreeParExcel.ajax.php", // url de la page à charger
        data: {"name":"John","date":"05 & 06 mars"},
        cache: false, // pas de mise en cache
        async: false, 
        contentType : "application/json",
        dataType: "json",
        success:function(jsonRetour){

        },
        error:function(XMLHttpRequest, textStatus, errorThrows){ // erreur durant la requete

        }
    });

And PHP code :

        $name = $_GET["nom"];
        $date = $_GET["date"];

And this one does not work

    var dataAjax = {};
    dataAjax["name"] = "John";
    dataAjax["date"] = "05 & 06 mars";
    var entree = JSON.stringify(dataAjax);

    $.ajax({ 
        url: "ajouterEntreeParExcel.ajax.php", // url de la page à charger
        data: entree,
        cache: false, // pas de mise en cache
        async: false, 
        contentType : "application/json",
        dataType: "json",
        success:function(jsonRetour){

        },
        error:function(XMLHttpRequest, textStatus, errorThrows){ // erreur durant la requete

        }
    });

With the same PHP code. In Debug with firebug, i check the variable "entree", and it is well formated, but i does not get anything in the PHP side.

nota : i prefer to use GET type and not POST type.

Any idea ?


Solution

  • So in order to have a version working with an object and with the responde given, this code work :

        var dataAjax = {};
        dataAjax["date"] = obj["Date"];
        dataAjax["comite"] = obj["Comité Int.Rég."];
    
    
        $.ajax({ 
            url: "ajouterEntreeParExcel.ajax.php", // url de la page à charger
            data: dataAjax,
            cache: false, // pas de mise en cache
            //async: false, 
            //contentType : "application/json",
            dataType: "json",
            success:function(jsonRetour){
                printValueTraitee = printValueTraitee + '<span class="green">OK</span>';
            },
            error:function(XMLHttpRequest, textStatus, errorThrows){ // erreur durant la requete
                printValueTraitee = printValueTraitee + '<span class="red">KO</span>';
            }
        });
    

    So i do not send my data with the JSON format anymore, but juste like an object without stringify it.