0

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

6
  • did you tried "/NewListe?liste="+JSON.stringify(this.myListe)? Commented Apr 2, 2021 at 8:58
  • 1
    The problem is that "/NewListe?liste="+this.myListe results 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.myListe is an object, and sending it to the server is done by turning it into a JSON string i.e. text. Commented Apr 2, 2021 at 8:59
  • @ManuelSpigolon Yes I tried. I still have a string variable, when I tried to log it, I have this : [,]. And if I want to parse it, I have the same error than before. Commented Apr 2, 2021 at 9:05
  • @ChrisG I tried by changing GET into POST on both side, and the problem still persist. Commented Apr 2, 2021 at 9:05
  • 1
    You're probably still sending it as part of the URL then. Use this.$http.post("/NewListe", { liste: this.myListe }) instead, make sure express has bodyparser middleware enabled, then use server.app.post(...) and check req.body.liste Commented Apr 2, 2021 at 9:06

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.