axios

Axios. How to get error response even when api return 404 error, in try catch finally

感情迁移 提交于 2019-12-01 15:38:56
问题 for e.g. (async() => { let apiRes = null; try { apiRes = await axios.get('https://silex.edgeprop.my/api/v1/a'); } catch (err) { console.error(err); } finally { console.log(apiRes); } })(); in finally , apiRes will return null. Even when the api get a 404 response, there is still useful information in the response that I would like to use. How can I use the error response in finally when axios throws error. https://jsfiddle.net/jacobgoh101/fdvnsg6u/1/ 回答1: According to the documentation, the

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

风流意气都作罢 提交于 2019-12-01 15:21:10
I have a React frontend that uses jwt to authenticate with the Django backend. The backend works and is connecting just fine using django views, but when I try to proxy a request from React, it gives me a Connection Refused error. Proxy error: Could not proxy request /api/auth/token/obtain/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED). Connecting to http://localhost:8000/api/auth/token/obtain/ works normally. And sending a POST request with Axios also works normally and returns the token json. But when I proxy it with node, it doesn't work. In my package.json I have: "proxy": {

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

二次信任 提交于 2019-12-01 14:16:40
问题 I have a React frontend that uses jwt to authenticate with the Django backend. The backend works and is connecting just fine using django views, but when I try to proxy a request from React, it gives me a Connection Refused error. Proxy error: Could not proxy request /api/auth/token/obtain/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED). Connecting to http://localhost:8000/api/auth/token/obtain/ works normally. And sending a POST request with Axios also works normally and returns

Dynamic host in axios

。_饼干妹妹 提交于 2019-12-01 14:12:21
How can I create a dynamic host in axios ? Example: const host = location.hostname; // axios.defaults.baseURL = 'http://hastore.local'; axios.defaults.baseURL = host; axios.defaults.port = 8080; axios.get('api/categories') .then((res) => { this.categories = res.data; console.log(res); }) .catch((err) => { console.warn('error during http call', err); }); String axios.defaults.baseURL = 'http://hastore.local'; does not fit, because on prodaction don't be work. String const host = location.hostname; also not a solution, because I get incorrect port and dublicate host. The goal is to get the right

Is there any alternative to CORS google chrome extension? How to make successful ajax request without using CORS?

折月煮酒 提交于 2019-12-01 14:06:09
Hi I have build Udemy API that fetches the courses using React and axios. It works fine if the chrome has CORS extension turned on but otherwise it does not fetch the data. I have already asked a question regarding this issue, please take a moment to read. I have tried all the solutions provided online.. Thanks Related issue: Failed to load resource. Origin is not allowed by Access-Control-Allow-Origin This problem usually related with the backend server, but if you don't have an access to the server so you have two options First option to use this chrome extension: Allow-Control-Allow-Origin

Set baseURL from .env in VueAxios

巧了我就是萌 提交于 2019-12-01 13:34:43
I am using a standard Vue starer webpack template with VueAxios installed. I want to configure axios using .env variable when building with dev command. I have everything set up within /config/index.js : const devEnv = require('./dev.env') module.exports = { ... host: devEnv.host || 'localhost', port: devEnv.port || 8080, ... } Then I define my host and port in dev.env.js in the same directory and it all works fine. const dotenv = require('dotenv').config({path: '../.env'}) let host = process.env.VUE_HOST; let port = +process.env.VUE_PORT; module.exports = merge(prodEnv, { NODE_ENV: '

Dynamic host in axios

自古美人都是妖i 提交于 2019-12-01 12:45:27
问题 How can I create a dynamic host in axios ? Example: const host = location.hostname; // axios.defaults.baseURL = 'http://hastore.local'; axios.defaults.baseURL = host; axios.defaults.port = 8080; axios.get('api/categories') .then((res) => { this.categories = res.data; console.log(res); }) .catch((err) => { console.warn('error during http call', err); }); String axios.defaults.baseURL = 'http://hastore.local'; does not fit, because on prodaction don't be work. String const host = location

Set baseURL from .env in VueAxios

风流意气都作罢 提交于 2019-12-01 10:16:23
问题 I am using a standard Vue starer webpack template with VueAxios installed. I want to configure axios using .env variable when building with dev command. I have everything set up within /config/index.js : const devEnv = require('./dev.env') module.exports = { ... host: devEnv.host || 'localhost', port: devEnv.port || 8080, ... } Then I define my host and port in dev.env.js in the same directory and it all works fine. const dotenv = require('dotenv').config({path: '../.env'}) let host = process

body data not sent in axios request

心已入冬 提交于 2019-12-01 07:33:24
I am trying to send data through axios request to my backend script, but the body looks empty. Here's a request sent from front-end: axios.request({ method: 'GET', url: `http://localhost:4444/next/api`, headers: { 'Authorization': token }, data: { next_swastik: 'lets add something here' }, }).then((res)=>{ console.log("api call sucessfull",res); }).catch((err)=>{ console.log("api call unsucessfull",err); this.props.toggleLoading(false); }) Here's a back-end: app.get('/next/api', verifyToken, function(req, res) { console.log(req.body); }) But I am getting {} empty body. I am getting headers and

Uploading files with VueJS, axios and Laravel

喜夏-厌秋 提交于 2019-12-01 06:45:57
Hello I am building one project. Where user can send up to 5 images and up to 10 songs with the text. But when I send request to the server, where I handle with Laravel, I can't get those files. // my data object from VueJS data() { return { formData: new FormData(), pikir: { body: '', }, isLoading: false, images: [], songs: [], } } // imagePreview method from VuejS imagePreview(event) { let input = event.target; if (input.files[0]) { if (input.files.length <= 5) { for (let i = 0; i < input.files.length; i++) { let reader = new FileReader(); reader.onload = e => { this.images.push(e.target