How to set state of response from axios in react

You have a syntax error here. You should try this instead var self = this; axios.get(‘/url’) .then(function (response) { console.log(response); self.setState({events: response.data}) }) .catch(function (error) { console.log(error); }); //the rest of the code var a=”i might be executed before the server responds” There are a few things to note here: axios.get is an asynchronous function … Read more

React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED)

So the issue was since both the Node dev environment and the Django dev environment were running in separate docker containers, so localhost was referring to the node container, not the bridged network. So the key was to use container links, which are automatically created when using docker-compose, and use that as the hostname. So … Read more

Axios can’t set data

In option functions like data and created, vue binds this to the view-model instance for us, so we can use this.contas, but in the function inside then, this is not bound. So you need to preserve the view-model like (created means the component’s data structure is assembled, which is enough here, mounted will delay the … Read more

How to manage axios errors globally or from one point

You should use an interceptor. First, create an axios instance using the create method. This is what you would need to use throughout your app instead of referencing axios directly. It would look something like this: let api = axios.create({ baseURL: ‘https://example.com/api/’, timeout: 1000, headers: {‘X-Custom-Header’: ‘foobar’} }); Then attach an interceptor to your axios … Read more

Download binary file with Axios

I was able to create a workable gist (without using FileSaver) as below: axios.get(“http://my-server:8080/reports/my-sample-report/”, { responseType: ‘arraybuffer’, headers: { ‘Content-Type’: ‘application/json’, ‘Accept’: ‘application/pdf’ } }) .then((response) => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement(‘a’); link.href = url; link.setAttribute(‘download’, ‘file.pdf’); //or any other extension document.body.appendChild(link); link.click(); }) .catch((error) => console.log(error));

Put request with simple string as request body

I solved this by overriding the default Content-Type: const config = { headers: {‘Content-Type’: ‘application/json’} }; axios.put(url, content, config).then(response => { … }); Based on my experience, the default Content-Type is application/x-www-form-urlencoded for strings, and application/json for objects (including arrays). Your server probably expects JSON.

sending file and json in POST multipart/form-data request with axios

To set a content-type you need to pass a file-like object. You can create one using a Blob. const obj = { hello: “world” }; const json = JSON.stringify(obj); const blob = new Blob([json], { type: ‘application/json’ }); const data = new FormData(); data.append(“document”, blob); axios({ method: ‘post’, url: ‘/sample’, data: data, })

What is Axios default timeout

According to the README, it is 0 which means no timeout // `timeout` specifies the number of milliseconds before the request times out. // If the request takes longer than `timeout`, the request will be aborted. timeout: 1000, // default is `0` (no timeout) https://github.com/axios/axios/blob/master/README.md#request-config

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)