Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue passing data to POST method

I have a problem with passing data to Vuejs post method. I'm using vue-resource and in documentation is:

this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);

I want to put header and it works fine(server reads header correct) for:

 this.$http.post(
      'http://localhost:8081/login', 
      {
        headers: {
          'Accept': 'application/json'
        }
      });

but if I try add any data something goes wrong, for example I tried:

this.$http.post(
      'http://localhost:8081/login',
       {username: 'admin'},
      {
        headers: {
          'Accept': 'application/json'
        }
      });

or define some data and put there like:

this.$http.post(
      'http://localhost:8081/login', 
       this.data,
      {
        headers: {
          'Accept': 'application/json'
        }
      });

and for both solutions body section is always empty - check body data from request by using:

 body = req.getReader().lines().collect(Collectors.joining(System.lineSeparator()));

Probably problem is in vue.js code ebcause it works fine it I send any request from Postman. What should I do?

like image 979
littlewombat Avatar asked Feb 19 '17 12:02

littlewombat


People also ask

How do I send data to Vue?

VueJS props are the simplest way to share data between components. Props are custom attributes that we can give to a component. Then, in our template, we can give those attributes values and — BAM — we're passing data from a parent to a child component!

How do I fetch API Vue?

Vue Fetch data from API exampleconst responsePromise = fetch(resourceUrl [, options]); The Response object we mention above represents the entire HTTP response, it does not directly contain the response body. To get the actual JSON body of the response, we use response. json() method.


1 Answers

Have you tried sending a post request without including the third argument with post

this.$http.post('url', {something: "string"})
  .then ((res)=> console.log (res.body))
  .catch ((error)=> console.log(error))

This works for me...

Another thing, are you sure you're getting the request data properly in the back end... it seems like python and I'm not totally sure how it works.

like image 188
peaceman Avatar answered Sep 19 '22 23:09

peaceman