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 ?
It's because in the first request you're sending the data as x-www-form-urlencoded
, which is what your PHP code is expecting:
name=John&date=05 & 06 mars
Whereas in the second one you're sending JSON formatted data in the request, eg:
'{"name":"John","date":"05 & 06 mars"}'
Also note that you should remove async: false
as it is considered horrendous practice to use it. If you check your console you will see a browser warning about its use.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.