Construction d'objet JSON
Bonjour,
j'essaie de construire un objet json format {'aa':'aa','zz':'zz','ee':'ee','list':{{'p1':'ppp','p2','zzz'},{'p1':'pp','p2','z'}} avant l'envoyer à mon action spring mvc.
voir le code ci dessous :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
function enregistrerCommande(){
var data={};
data['numeroContrat']= $('#numeroContrat').val();
data['numeroCommande']= $('#numeroCommande').val();
data['objet']= $('#objet').val();
data['description']= $('#description').val();
var detailsCommande=[];
for(i=0;i<=compteur;i++){
var detail ={};
detail['typeProduit']=$('#typeProduit'+i).val();
detail['quantite']=$('#quantite'+i).val();
detail['prix']=$('#prixunit'+i).val();
detailsCommande.push(detail);
}
var detailsCommandeForm = JSON.stringify(detailsCommande);
data['detailsCommandeForm']=detailsCommandeForm;
console.log(JSON.stringify(data));
$.ajax({
type:'post',
contentType: 'application/json',
data:JSON.stringify(data),
dataType: 'json',
url:'${pageContext.request.contextPath}/saisircommande/enregistrer',
success:function(response){
console.log(response);
},
error:function(response){
alert('error');
}
});
} |
lorsque j'envoie ma requête, j'obtient une erreur 405. mon objet json construit est:
Citation:
{"numeroContrat":"1","numeroCommande":"ff","objet":"sdds","description":"fdfsd","detailsCommandeForm":"[{\"typeProduit\":\"1\",\"quantite\":\"1\",\"prix\":\"2\"}]"}
je pense que le format de mon objet json qui cause cette erreur.
comment faire pour qu'il soit au format mentionné ci dessus ?
Merci.