📜  从单独的文件返回 axios 响应 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:16.398000             🧑  作者: Mango

代码示例3
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);
});