react-native request wrapper

Here is the axios plugin based on ES6 promise, of course, you need to download this plugin first

npm install axios

1. First create an http folder in the follow directory and create index.js in it. The code is as follows:

import React, { Component } from 'react';
import axios from 'axios'

let token = '';

var instance = axios.create({
    baseURL: 'https://baidu.com' ,
    timeout: 5000,
    headers: {'X-Custom-Header': 'foobar'},
     // `transformResponse` allows changes to the response data to be made before
    // it is passed to then/catch
    transformResponse: [function (data) {
        /**
         * Handle exceptions uniformly through the returned data
         */
        return data;
        // Alter defaults after instance has been created 处理token
        instance.defaults.headers.common['Authorization'] = token;    
    }],
});

export default instance;

2. When you need to use the requested page method as follows:

import axios from '../http' //Introduce the index file above
  // this.props.navigation.navigate('Page4',params); specific request method 
        axios.get('/api/cms/eleser/homePage' )
            .then(res => {
                console.log(res);
            }), err => {
                console.log(`${err}`)
            }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325271367&siteId=291194637
Recommended