Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  85] [ 7]  / answers: 1 / hits: 64311  / 7 Years ago, thu, may 4, 2017, 12:00:00

I am trying to post my form using axios, but I am not able to get the data to my backend using expressjs



This is what I am doing:



<template>
<form class= method=post @submit.prevent=postNow>
<input type=text name= value= v-model=name>
<button type=submit name=button>Submit</button>
</form>
</template>

export default {
name: 'formPost',
data() {
return {
name: '',
show: false,
};
},
methods: {
postNow() {
axios.post('http://localhost:3030/api/new/post', {
headers: {
'Content-type': 'application/x-www-form-urlencoded',
},
body: this.name,
});
},
components: {
Headers,
Footers,
},
};


backend file:



app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
router.post('/new/post', (req, res) => {
res.json(console.log(this is working + ' ' + req.body.name));
});


The error I am receiving is:



this is working undefined

More From » node.js

 Answers
2

Axios post format:



axios.post(url[, data[, config]])



Your request should be:



axios.post('http://localhost:3030/api/new/post', 
this.name, // the data to post
{ headers: {
'Content-type': 'application/x-www-form-urlencoded',
}
}).then(response => ....);


Fiddle: https://jsfiddle.net/wostex/jsrr4v1k/3/


[#57897] Tuesday, May 2, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zackeryzainv

Total Points: 61
Total Questions: 102
Total Answers: 99

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;