创建axios界面并从其他文件调用

时间:2019-01-11 11:46:46

标签: javascript promise axios redux-saga

我正在尝试创建一个服务实用程序,以使用来自redux / saga的axios进行http调用。

但是此函数总是返回未定义的

serviceutils.js

import axios from 'axios'

const apiV1 = axios.create({
    baseURL: 'https://google.com',
    timeout: 2000,
    headers: { 'X-Custom-Header': 'foobar' }
});

-

import apiV1 from './../Utils/Service.Utils'
saga.js
function* fetchdata() {
  yield apiV1.post("http://google.com")
}

3 个答案:

答案 0 :(得分:1)

据我所知,Axios以基于Promise的方式运行,因此您可以使用async / await或Promise的then / catch链。

可以在另一个文件中调用Axios,然后在第三个文件中使用它,但这取决于上述技术。请检查以下文档:

https://github.com/axios/axios

答案 1 :(得分:0)

you have need to return the function after done the execution or 
getting the response.

import apiV1 from './../Utils/Service.Utils'
saga.js
function* fetchdata() {
  return apiV1.post("http://google.com").then(function (response) {
    return response;
  })
  .catch(function (error) {
    return error;
  })
}

答案 2 :(得分:0)

我已经解决了拦截器阻止响应的问题,并返回了未定义的