Following the fetch API in https://developer.mozilla.org/es/docs/Web/API/Fetch_API/Using_Fetch I write the following method in VUE, I am working locally, and with VUE and VUETIFY calling them by CDN, without installation npm.
<body>
....
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
<script src="./productes.js"></script>
</body>
imprimir()
{
var fetchData =
{
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({'comanda': this.comanda})
}
fetch('./imprimir.php', fetchData)
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log("print pdf -> ", data);
})
.catch(function(err) {
console.error(err);
});
},
The response in print.php, which now I only have to print the $_POST on the screen.
$posted_data = $_POST['comanda'];
$data = json_decode($posted_data);
echo $data;
print_r($data);
The call seems correct, but the $_POST is empty. The parameter passed by POST is of the form:
0: {referencia: "p_0001", producte: "Producte 1", quantitat: 1, preu: "2.00", iva: "4.00", base: 2,…}
base: 2
iva: "4.00"
preu: "2.00"
producte: "Producte 1"
quantitat: 1
referencia: "p_0001"
total: "2.08"
totaliva: "0.08"
Why is the $_POST empty?
In the Request Payload it shows me:
{"comanda":[{"referencia":"p_0001","producte":"Producte 1","quantitat":1,"preu":"2.00","iva":"4.00","base":2,"totaliva":"0.08","total":"2.08"},{"referencia":"p_0002","producte":"Producte 2","quantitat":1,"preu":"4.00","iva":"4.00","base":4,"totaliva":"0.16","total":"4.16"},{"referencia":"p_0003","producte":"Producte 3","quantitat":1,"preu":"6.00","iva":"10.00","base":6,"totaliva":"0.60","total":"6.60"}]}
And doing console.log(this.comanda) as the first instruction within print shows me this in the console:
(3) [{…}, {…}, {…}, __ob__: Observer]
0: base: 2iva: "4.00"preu: "2.00"producte: "Producte 1"quantitat: 1referencia: "p_0001"total: "2.08"totaliva: "0.08"__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}get base: ƒ reactiveGetter()set base: ƒ reactiveSetter(newVal)get iva: ƒ reactiveGetter()set iva: ƒ reactiveSetter(newVal)get preu: ƒ reactiveGetter()set preu: ƒ reactiveSetter(newVal)get producte: ƒ reactiveGetter()set producte: ƒ reactiveSetter(newVal)get quantitat: ƒ reactiveGetter()set quantitat: ƒ reactiveSetter(newVal)get referencia: ƒ reactiveGetter()set referencia: ƒ reactiveSetter(newVal)get total: ƒ reactiveGetter()set total: ƒ reactiveSetter(newVal)get totaliva: ƒ reactiveGetter()set totaliva: ƒ reactiveSetter(newVal)__proto__: Object1: {…}
2: base: (...)iva: (...)preu: (...)producte: (...)quantitat: (...)referencia: (...)total: (...)totaliva: (...)__ob__: Observer {value: {…}, dep: Dep, vmCount: 0}get base: ƒ reactiveGetter()set base: ƒ reactiveSetter(newVal)get iva: ƒ reactiveGetter()set iva: ƒ reactiveSetter(newVal)get preu: ƒ reactiveGetter()set preu: ƒ reactiveSetter(newVal)get producte: ƒ reactiveGetter()set producte: ƒ reactiveSetter(newVal)get quantitat: ƒ reactiveGetter()set quantitat: ƒ reactiveSetter(newVal)get referencia: ƒ reactiveGetter()set referencia: ƒ reactiveSetter(newVal)get total: ƒ reactiveGetter()set total: ƒ reactiveSetter(newVal)get totaliva: ƒ reactiveGetter()set totaliva: ƒ reactiveSetter(newVal)__proto__: Objectlength: 3__ob__: Observer {value: Array(3), dep: Dep, vmCount: 0}__proto__: Array
One of the things I have tried is to make a post with Python in the following way:
import requests
url = 'http://localhost/php_py/imprimir.php'
datos = {'num': 1}
x = requests.post(url,data=datos)
print(x.text)
With this, the Php does print the received message:
Array
(
[num] => 1
)
I also add that if I do a GET in the fetch, in this way:
fetch('http://localhost/php_py/imprimir.php?comanda='+JSON.stringify(this.comanda))
.then(response => response.json())
.then(json => console.log(json))
.catch(err => console.log(err));
If I receive the data in the Php, and I can see it when doing the print_r($_GET)
I have resolved the issue by capturing the POST as follows:
Viewed at https://stackoverflow.com/questions/55250688/fetch-doesnt-send-post-data
But I don't quite understand why the file_get_contents is necessary..