Reputation: 75
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
Upvotes: 0
Views: 109
Reputation: 899
You should surely be using a POST method if you are sending JSON data to the server - a GET just isn't designed for that sort of usage.
Upvotes: 1
Reputation: 1
Since you have passed a JSON in the url, it will be URLEncoded. So, in the backend before you do JSON.parse(liste)
, you should do decodeURI(liste)
. decodeURI()
will return the JSON string which you can parse and use it in your code. I hope this will fix your problem.
Upvotes: 0