svrcekmichal / redux-axios-middleware

Redux middleware for fetching data with axios HTTP client
MIT License
920 stars 96 forks source link

Update doc #50

Closed ihorml closed 7 years ago

ihorml commented 7 years ago

Please update your README from this one:

const client = axios.create({ //all axios can be used, shown in axios documentation
  baseURL:'http://localhost:8080/api',
  responseType: 'json'
});

To this one:

const client = axios.create({ //all axios can be used, shown in axios documentation
  baseURL:'http://localhost:8080/api',
  options: {   
    responseType: 'json'
  }
});

As I see from this line you use options for Axios from client.options. So If I would like to use responseType:json I should put it to payload.options or client.options.

I just spent a lot of time on investigating why I receive the following error:

axios Failed to read the 'responseText' property from 'XMLHttpRequest': The value is only accessible if the object's 'response Type' is '' or 'text' (was 'json').

So the solution was just simple - put responseType:json to client.options.

nmaves commented 7 years ago

I don't believe that our docs are incorrect.

Here is just one of my working examples.

export const client = axios.create({
    baseURL: config[ KEY ],
    withCredentials: true,
    headers: {
        'ws-api': '2.1',
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: [ function (data) {
        var str = []
        for (let p in data)
            if (data.hasOwnProperty(p) && data[ p ] != null) {
                str.push(encodeURIComponent(p) + '=' + encodeURIComponent(data[ p ]))
            }
        return str.join('&')
    } ]
})

Notice the axios configuration parameters are not nexted inside of the options.

Are you trying to configure multiple clients using this?

ihorml commented 7 years ago

@nmaves I'm talking about JSON requests (responseType:json, contentType: application/json;charset=utf-8) when you receive an error and handle it by .catch like:

someReduxAxiosAction().then((response) => {
}).catch((err) => {
});

On this way you receive error I noticed on Topic. If it's matter I can provide you an example a little bit later.

nmaves commented 7 years ago

Are you setting returnRejectedPromiseOnError to true?

ihorml commented 7 years ago

@nmaves yes. I'll try setup an example for you