I'm trying to send user data from Nuxt.js using Axios via a POST
request. The data is already provided via a Javascript cdn function that returns an object with user
parameters, so I wouldn't want to use a form
since I'm forwarding the user
data I received as JSON
.
I wanted to know if the method I'm using is the right way of doing this? I need to send the user
information in order to send a query in the backend to an external API (requiring a token from both the front and the back end, e.g., user token and app token).
Here is my current iteration:
<script>
export default {
head (){
return {
__dangerouslyDisableSanitizers: ['script'],
script: [
{
hid: 'platform-api',
src: "https://cdn-sample.app.com/api",
type: 'text/javascript',
defer: true
},
]
}
},
computed: {
// Change user token parameter according to docs
// Add Neccessary parameters
auth_token: {
get(){
let userdata = getPlatformContext();
this.$store.state.user.auth_token = userdata.auth_token;
return this.$store.state.user.auth_token;
},
set(value){
this.$store.commit("item/storeAuthToken", value)
}
},
// Additional parameters omitted as they extract each parameter in the same way
// as above.
methods: {
// I tried to test it by sending just the user token by clicking a button
async sendUserToken(auth_token) {
await this.$axios.post(this.$config.baseURL, user.auth_token);
},
// Then i wanted instead to try and send the whole json dict of user data to
// backend and sort the data over in fastapi according to what i need.
async sendUserData(user) {
await this.$axios.post(this.$config.baseURL, user);
}
},
}
</script>
So, if I wanted to send the user data as a POST
request in JSON
format, not as a form
, what would be the best way to do this?