1var formData = new FormData();
2var imagefile = document.querySelector('#file');
3formData.append("image", imagefile.files[0]);
4axios.post('upload_file', formData, {
5 headers: {
6 'Content-Type': 'multipart/form-data'
7 }
8})
9
1import axios from 'axios';
2
3export function getData(config, callback, errorcallback){
4 axios.get(url, config)
5 .then(res => {
6 //do something
7 if(callback != null){
8 callback(res);
9 }
10 })
11 .catch(err => {
12 // catch error
13 if(errorcallback != null){
14 errorcallback(err);
15 }
16 })
17}
18In any component, use as follows
19
20// get the location of your apicalls.js file and use to import like below
21import { getData } from '../../routetothisjsfile'
22
23
24//use it
25var config = { "Access-Control-Allow-Origin": "*" }
26getData(config, (res) => {
27 //success
28},(err) => {
29 //error
30 alert(err);
31});
32
1import axios from 'axios';
2
3export const onAuthenticate = payload => {
4 const URL = `YOUR_URL`;
5 return axios(URL, {
6 method: 'POST/GET',
7 headers: {
8 'content-type': 'application/json', // whatever you want
9 },
10 data: payload,
11 })
12 .then(response => response.data)
13 .catch(error => {
14 throw error;
15 });
16};
17in you App.js
18
19import * as AuthenticateAPI from 'api/AuthenticationAPI';
20
21 // in your CDM
22 componentDidMount(){
23 AuthenticateAPI.onAuthenticate(payload).then((res)=>{ //any payload you want to send just for example
24 you can get response here in then block
25 })
26 }