Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

return axios response from seperate file

import axios from 'axios';

export const onAuthenticate = payload => {
  const URL = `YOUR_URL`;
  return axios(URL, {
    method: 'POST/GET',
    headers: {
      'content-type': 'application/json', // whatever you want
    },
    data: payload,
  })
    .then(response => response.data)
    .catch(error => {
      throw error;
    });
};
in you App.js

import * as AuthenticateAPI from 'api/AuthenticationAPI';

 // in your CDM
 componentDidMount(){
  AuthenticateAPI.onAuthenticate(payload).then((res)=>{ //any payload you want to send just for example
    you can get response here in then block
 }) 
 }
Comment

return axios response from seperate file

import axios from 'axios';

export function getData(config, callback, errorcallback){
    axios.get(url, config)
    .then(res => {
      //do something
      if(callback != null){
         callback(res);
      }
    })
    .catch(err => {
      // catch error
      if(errorcallback != null){
         errorcallback(err);
      }
    })
}
In any component, use as follows

// get the location of your apicalls.js file and use to import like below
import { getData } from '../../routetothisjsfile'


//use it 
var config = { "Access-Control-Allow-Origin": "*" }
getData(config, (res) => {
    //success
},(err) => {
    //error
    alert(err);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: customize bar in chartjs 
Javascript :: vue js key modifiers 
Javascript :: how to translate the title in js file in magento 2 
Javascript :: gsheet calculate next tuesday date 
Javascript :: how to get value from select option using input name in jquery 
Javascript :: how to bind two ng-content in a component angular 
Javascript :: discord javascript error on startup 
Javascript :: serve public folder express without file extension 
Javascript :: nodejs css cotent tipe 
Javascript :: how to store and get a single variable in local storage js 
Javascript :: swift read json from url 
Javascript :: caeser psypher javascript 
Javascript :: for of exemple 
Javascript :: var quotes 
Javascript :: visio prevent arrows from snapping 
Javascript :: Reading manifest: Warning processing Description: An unexpected property was found in the WebExtension manifest. 
Javascript :: rebuild package-lock.json 
Javascript :: get values from string with delimiter google script 
Javascript :: tower defense bullet following enemy with range javascript 
Javascript :: please run 
Javascript :: Create Compond from route to js function communication 
Javascript :: vonage singal api 
Javascript :: how to do something before every method is run in a class javascript 
Javascript :: nodejs cors 
Javascript :: how to check if .each first element 
Javascript :: release mouse key javascript 
Javascript :: on device size changes react-multi-carousel items not showing 
Javascript :: "Perform native operation by javascript in Android" 
Javascript :: call function on scroll down javascript 
Javascript :: how to check jasmine version 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =