axios

how to return response of axios in return

巧了我就是萌 提交于 2019-12-03 07:35:57
问题 I want to return the response of axios but always the response that returned is undefined: wallet.registerUser=function(data){ axios.post('http://localhost:8080/register',{ phone:data.phone, password:data.password, email:data.email }).then(response =>{ return response.data.message; console.log(response.data.message); }).catch(err =>{ console.log(err); }) } console.log(wallet.registerUser(data)); The console always logs as undefined. Is their any way returning this response. 回答1: console.log

Multiple fields with same key in query params (axios request)?

时光怂恿深爱的人放手 提交于 2019-12-03 04:38:25
So the backend (not under my control) requires a query string like this: http://example.com/?foo=5&foo=2&foo=11 But axios uses JS object to send the request params: axios.get('http://example.com/', { foo: 5 }); And obviously such object can't have multiple fields with the same key. How can I send a request with multiple fields with same key? From the axios documentation on the request config // `params` are the URL parameters to be sent with the request // Must be a plain object or a URLSearchParams object params: { ID: 12345 }, To use this in a request, you would do var request = { params: {

How to redirect from axios interceptor with react Router V4?

徘徊边缘 提交于 2019-12-03 04:22:47
问题 I want to make a redirection in axios interceptors when receiving a 403 error. But how can I access the history outside React components ? In Navigating Programatically in React-Router v4, it's in the context of a React Component, but here i'm trying in axios context axios.interceptors.response.use(function (response) { // Do something with response data return response; }, function (error) { // Do something with response error if(error.response.status === 403) { console.log("Redirection

Vue-Router Passing Data with Props

混江龙づ霸主 提交于 2019-12-03 00:37:08
I am having a hard time passing props using Vue-Router. I seem to not be able to access the props when I bring them into the next view. This is my methods object: methods: { submitForm() { let self = this; axios({ method: 'post', url: url_here, data:{ email: this.email, password: this.password }, headers: { 'Content-type': 'application/x-www-form-urlencoded; charset=utf-8' } }).then(function(response) { self.userInfo = response.data; self.$router.push({name: 'reading-comprehension', props: {GUID:self.userInfo.uniqueID }}); }) } } The post request is working, but when I try to route to a new

Using Axios in VueJS - this undefined

筅森魡賤 提交于 2019-12-02 23:05:49
问题 Using typescript and vuejs + axios, i have the following .catch function on a post request - I'm attempting to trap some network errors and report status to the end user: .catch(function(error) { console.error(error.response); if ( error.message === "Network Error" ) { this.alert.show = 1; this.alert.message = error.message + ': Please try again later'; } }); this.alert.show throws "this" undefined in the debugger. Is this a problem with javascript/typescript and exception handlers in general

How can you use axios interceptors?

流过昼夜 提交于 2019-12-02 22:29:46
I have seen axios documentation, but all it says is // Add a request interceptor axios.interceptors.request.use(function (config) { // Do something before request is sent return config; }, function (error) { // Do something with request error return Promise.reject(error); }); // Add a response interceptor axios.interceptors.response.use(function (response) { // Do something with response data return response; }, function (error) { // Do something with response error return Promise.reject(error); }); Also many tutorials only show this code but I am confused what it is used for, can someone

how to return response of axios in return

我只是一个虾纸丫 提交于 2019-12-02 21:11:30
I want to return the response of axios but always the response that returned is undefined: wallet.registerUser=function(data){ axios.post('http://localhost:8080/register',{ phone:data.phone, password:data.password, email:data.email }).then(response =>{ return response.data.message; console.log(response.data.message); }).catch(err =>{ console.log(err); }) } console.log(wallet.registerUser(data)); The console always logs as undefined. Is their any way returning this response. Showard180 console.log won't wait for the function to fully complete before logging it. This means that you will have to

JS Axios - how to get response body in event of error?

梦想与她 提交于 2019-12-02 19:59:12
问题 I want to be able to get the response body back from an Axios error catch. I am using axios v0.18.0. My axios code looks like this: let service = axios.create({ baseURL: "https://baseUrl.azurewebsites.net", responseType: "json" }); service.defaults.headers.common['authorization'] = `Bearer ${token}`; service.post("/api/method", params).then(result => { console.log('success',result); }).catch(error=>{ console.log('error'); console.log(error); }); My API call is returning a 400 error as I

Axios spread() with unknown number of callback parameters

不问归期 提交于 2019-12-02 19:07:11
I need to process an unknown number of AJAX requests (1 or more) with axios, and I am not sure how to handle the response. I want something along the lines of: let urlArray = [] // unknown # of urls (1 or more) axios.all(urlArray) .then(axios.spread(function () { let temp = []; for (let i = 0; i < arguments[i].length; i++) temp.push(arguments[i].data); })); where arguments will contain the callback responses sent by axios. The problem is that arguments contains the given string urls instead of the actual responses. How can I resolve this problem? You somewhere will need to make the actual

How to redirect from axios interceptor with react Router V4?

不羁的心 提交于 2019-12-02 17:46:27
I want to make a redirection in axios interceptors when receiving a 403 error. But how can I access the history outside React components ? In Navigating Programatically in React-Router v4 , it's in the context of a React Component, but here i'm trying in axios context axios.interceptors.response.use(function (response) { // Do something with response data return response; }, function (error) { // Do something with response error if(error.response.status === 403) { console.log("Redirection needed !"); } // Trow errr again (may be need for some other catch) return Promise.reject(error); }); I