axios

How to catch and handle error response 422 with Redux/Axios?

半世苍凉 提交于 2019-12-04 07:40:57
问题 I have an action making a POST request to the server in order to update a user's password, but I'm unable to handle the error in the chained catch block. return axios({ method: 'post', data: { password: currentPassword, new_password: newPassword }, url: `path/to/endpoint` }) .then(response => { dispatch(PasswordUpdateSuccess(response)) }) .catch(error => { console.log('ERROR', error) switch (error.type) { case 'password_invalid': dispatch(PasswordUpdateFailure('Incorrect current password'))

How do I use axios response in different components without using export?

狂风中的少年 提交于 2019-12-04 06:04:16
问题 As the tittle says, I would like to be able to use the same axios response for differents components. I have some restrictions like, I'm onlyl able to use react by adding scripts tags to my html so things like exports or jsx are impossible for me. This is my react code: class User extends React.Component { state = { user: {} } componentWillMount() { console.log(localStorage.getItem("user")) axios.get('http://localhost:8080/dashboard?user=' + localStorage.getItem("user")) .then(res => { const

Axios request interceptor not working

孤者浪人 提交于 2019-12-04 05:18:41
I'm trying to make axios working with a request interceptor. However before a request is made the interceptor is not triggered. What could be going wrong here? I've red already a lot about this problem but not found a solution so far. Could use some help here! This is my code: import VueRouter from 'vue-router'; import Login from './components/Login.vue' import Home from './components/Home.vue' import axios from 'axios'; window.Vue = require('vue'); window.axios = axios.create({ baseURL: 'http://localhost:8080', timeout: 10000, params: {} // do not remove this, its added to add params later in

run response interceptor only once when multiple API calls

喜夏-厌秋 提交于 2019-12-04 05:18:25
I have an interceptor like this axios.interceptors.response.use(undefined, err=> { const error = err.response; console.log(error); if (error.status===401 && error.config && !error.config.__isRetryRequest) { return axios.post(Config.oauthUrl + '/token', 'grant_type=refresh_token&refresh_token='+refreshToken, { headers: { 'Authorization': 'Basic ' + btoa(Config.clientId + ':' + Config.clientSecret), 'Content-Type': 'application/x-www-form-urlencoded,charset=UTF-8' } }) .then(response => { saveTokens(response.data) error.config.__isRetryRequest = true; return axios(error.config) }) } }) And

Send “raw” payload to Axios

夙愿已清 提交于 2019-12-04 05:00:31
How can I send a raw payload/request body to Axios? The endpoint I'm trying to call expects the request body to just be a string which it'll scoop up and use. If I try to just pass a string to axios.post() for the requestBody , it'll convert it to an object with no value ( { "this+is+my+message": "" } ) and ends up getting parsed like this "this+is+my+message=" . I checked the documentation, but couldn't find any option that seemed to work. transformRequest seemed to be the most obvious, but it sent in the string and I sent out the string (literally d => d ), but it still seemed to convert it

cross domain at axios

馋奶兔 提交于 2019-12-04 04:56:19
I am changing jquery ajax for axios and I not getting use axios with cross domain: axios.get(myurl, { headers: { 'crossDomain': true }, }).then(res => { console.log(res); }).catch(error => { console.log('erro', error); }) My jquery code is working: $.ajax({ type: 'GET', crossDomain: true, url:myurl, success: (res) => {}, error: (fail) => {} }) The error: Request header field crossDomain is not allowed by Access-Control-Allow-Headers in preflight response. Can anyone help me? "crossDomain" does not have to be in headers axios.get(myurl, { crossDomain: true }).then(res => { console.log(res); })

Axios onUploadProgress only fires once on my machine

删除回忆录丶 提交于 2019-12-04 04:35:32
问题 If I use this fiddle https://jsfiddle.net/v70kou59/1/ everything works as expected (function () { var output = document.getElementById('output'); document.getElementById('upload').onclick = function () { var data = new FormData(); data.append('foo', 'bar'); data.append('file', document.getElementById('file').files[0]); var config = { onUploadProgress: function(progressEvent) { var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total ); console.log(percentCompleted

Axios Request Interceptor wait until ajax call finishes

别来无恙 提交于 2019-12-04 04:15:24
I have an request interceptor for axios calls. It checks my jwt token and call for refresh if necessary. axios.interceptors.request.use((config) =>{ const state = store.getState(); // get renewed state const time = Math.floor( new Date().getTime() / 1000 ); if( ! state.app.jwtRefreshOnRequest && time >= state.jwt.expires - 120 && state.jwt.refresh_before > time ){ // expiring in 2 min. refresh //dispatch({type: 'JWT_REFRESH_REQUEST'}); axios.get( API_BASE_URL + '/auth/refresh') .then(function(response){ // dispatch({type: 'JWT_REFRESH_SUCCESS', payload: response.data}); axios(config).then

Getting 403 (Forbidden) when uploading to S3 with a signed URL

二次信任 提交于 2019-12-04 02:54:46
I'm trying to generate a pre-signed URL then upload a file to S3 through a browser. My server-side code looks like this, and it generates the URL: let s3 = new aws.S3({ // for dev purposes accessKeyId: 'MY-ACCESS-KEY-ID', secretAccessKey: 'MY-SECRET-ACCESS-KEY' }); let params = { Bucket: 'reqlist-user-storage', Key: req.body.fileName, Expires: 60, ContentType: req.body.fileType, ACL: 'public-read' }; s3.getSignedUrl('putObject', params, (err, url) => { if (err) return console.log(err); res.json({ url: url }); }); This part seems to work fine. I can see the URL if I log it and it's passing it

Authorization header doesn't get sent in cross-origin request

寵の児 提交于 2019-12-04 02:44:55
问题 Solution at the END. My server was my problem... Shortly solution, I need to use express cors Original POST: First of ALL: I have add Access-Control-Allow-Origin, Access-Control-Allow-Headers and i can access the server, but, i can't send data in the Header to the server. So: I am developing basically with Vue.js (with webpack to hot reload changes) and Express (with nodemon). So I have 2 separated servers: First: backend (server) running at: http://localhost:3000 with nodemon Second: