I try to send data to my NodeJS server using HTTP protocol (vue-resource). I want to send a array of JSON object like this : [{"name":"Charlotte","surname":"Chacha","birth":"2000-04-02"},{"name":"Michael","surname":"Mic","birth":"1999-01-30"}].
My front code :
window.onload = function () {
var gamme = new Vue({
el:'#gamme',
data: {
myListe: []
},
methods: {
sendListe: function() {
this.$http.get("/NewListe?liste="+this.myListe).then(response=> {
if (response.body) {
console.log(response.body);
}
});
}
}
})
}
And my back code :
server.app.get("/NewListe", function(req, res) {
try {
let liste= req.query.liste;
console.log(liste);
} catch (e) {
console.log(e);
}
})
When I try to display the variable liste in the server side console, I obtain this : [object Object] . liste is a string type that I can't use. I would like to have an array of JSON, like in front.
I tried to parse like this JSON.parse(operationsGamme) , but I have this error : SyntaxError: Unexpected token o in JSON at position 1
"/NewListe?liste="+JSON.stringify(this.myListe)?"/NewListe?liste="+this.myListeresults in"/NewListe?liste=[object%20Object]". You can use stringify but a better solution is to use a POST request instead. Note that there's no such thing as a "JSON object".this.myListeis an object, and sending it to the server is done by turning it into a JSON string i.e. text.[,]. And if I want to parse it, I have the same error than before.this.$http.post("/NewListe", { liste: this.myListe })instead, make sure express has bodyparser middleware enabled, then use server.app.post(...) and checkreq.body.liste