简体   繁体   中英

How should an AXIOS post handler should look like if I don't use <form action=“” method=“POST”>

I am curios how exactly should an AXIOS post method should look like when you're not using an html form .

I did the following for a multiple file upload:

<template>
  <div @drop.prevent="drop"></div>
</template>
<script>
import axios from "axios";
const serverURL = location.origin;
const server = axios.create({ baseURL: serverURL, timeout: 5000 });

drop(event) {
// take files from event
// create formData with files
  try {
    await server.post("/files", formData, {
      headers: {
        "Content-Type": "multipart/form-data",
      },
      onUploadProgress: (event) =>
        (this.progress = Math.round((event.loaded * 100) / event.total)),
    });
  } catch (error) {
      console.error(error);
  }
}
</script>

I basically set only the "Content-Type" header.

Is that OK? Is it enough?

If I want to post text/strings from inputs is it enough to set:

      headers: {
        "Content-Type": "text/plain; charset=utf-8",
      },

Does form adds something else that I should also add?

Try below:

const serverURL = location.origin;   
axios.post(serverURL+"/someEndPoint", dataToSend, { headers: { 'Content-Type': 'text/plain; charset=utf-8' } }).then((res) => {
   console.log(res)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM