Bonjour,
Là, je n'arrive pas à comprendre :
Un fetch fonctionne en méthode GET mais comme j'ai pas mal de données (enfin, pas dans mon exemple) et que mon JSON est tout prêt, je veux le faire en POST.
Ca ne fonctionne que sur un FormData et pas sur un JSON. Dans ce dernier cas, mon $_POST en php est vide.
Ca doit être tellement bête que je vais devoir aller me cacher au fond des bois quand j'aurai une solution mais, dans l'immédiat, ça me prend la tête.
Problème reproduit sur winamp (php 5.6 et 7.1) et sur un serveur o2swicth.
att.html
Code html : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>title</title> <script src="att.js"></script> </head> <body> Test <script>go()</script> </body> </html>
att.js
att.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 async function go() { const formData=new FormData(); formData.append('title', 'My Vegas Vacation'); const jsonData={cmd:"test"}; console.log("POST FORM"); try { const response=await fetch('att.php',{method:'POST',body:formData}); const result=await response.json(); console.log('Success:',JSON.stringify(result)); } catch (error) { console.error('Error:',error); } console.log("POST JSON"); try { const response=await fetch('att.php',{headers:{'Accept': 'application/json','Content-Type': 'application/json'},method:'POST',body:JSON.stringify(jsonData)}); const result=await response.json(); console.log('Success:',JSON.stringify(result)); } catch (error) { console.error('Error:', error); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 <?php $data=$_POST; $data['org']=$data['title']; $data['title']='retour'; header('Content-type: application/json'); echo json_encode($data); ?>
Partager