axios

How to update state in react synchronously

。_饼干妹妹 提交于 2019-12-08 09:18:35
问题 onSave=()=>{ if (this.state.intialValue<=0) { this.setState({errorIntialValue: true}) } else { this.setState({errorIntialValue: false}) } if (this.state.age>=25|| this.state.age<=-1) { this.setState({errorAge: true}) } else{ this.setState({errorAge: false}) } if (this.state.rollNo<0) { this.setState({errorRollno: true}) } else{ this.setState({errorRollno: false}) } if(!(this.state.errorIntialValue|| this.state.errorAge ||errorRollno)){ //have to enter only if no error let newData={

React testing onSubmit using axios

ぐ巨炮叔叔 提交于 2019-12-08 08:10:07
问题 I recently started testing my React app. However, I stumbled when dealing with submitting forms. My test covers most of the lines but misses out on actual part of submit form method . LoginForm.js - submit form const userLoginData = { userId : this.state.userId, password : this.state.password, userType : this.state.userType }; axios({ data : JSON.stringify(userLoginData), type : 'post', url : Constant.BASE_URL_SERVER+'/rest/login', headers : { 'Accept': 'application/json', 'Content-Type':

React Warning: Can't call setState (or forceUpdate) on an unmounted component

☆樱花仙子☆ 提交于 2019-12-08 07:52:34
问题 I have 2 components: Orders - fetch some data and display it. ErrorHandler - In case some error happen on the server, a modal will show and display a message. The ErrorHandler component is warrping the order component I'm using axios package to load the data in Orders component, and i use axios interceptors to setState about the error, and eject once the component unmounted. When i navigate to the orders components back and forward i sometimes get an error in the console: Warning: Can't call

How to display data from api in react component

让人想犯罪 __ 提交于 2019-12-08 07:51:17
问题 I am using axios to get data from an API and the retrieved data was successfully dispatched to the store, I currently have some problems in displaying the data in my React component. I was able to console.log the data successfully but it turns out my component renders before the data was fully retrieved from the API. Below is the component to display the data: import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import { connect } from 'react-redux'; import PropTypes

async GET request with body from browser

流过昼夜 提交于 2019-12-08 07:51:08
问题 Ok, I know it's a bad idea and it shouldn't be done but for the sake of this question please assume there's no other way - I am given API endpoint that requires GET request with empty object as a body. Is there a way to do async request from browser? I'm using axios library which uses XMLHttpRequest under the hood and MDN says that send wipes the body when HTTP method is GET . I tried using native fetch but it gives me this error in browser: TypeError: Failed to execute 'fetch' on 'Window':

How to fix “Mixed Content:” error when making API calls to elasticsearch from react

大城市里の小女人 提交于 2019-12-08 06:57:02
问题 I am using the bitnami version of elasticsearch from firebase. I found out that it only connects to http and not https when i tried it using postman. My create-react-app is deployed to firebase and I am getting Mixed Content: error when I try to fetch data using the axios library. Here id my code: import axios from 'axios'; axios.get('http://xx.xxx.xx.xxx//elasticsearch/tasks/task/_search?default_operator=AND&q=*', { httpsAgent: agent, method: 'get', headers: { // 'Content-Type': 'application

Progress bar for superagent file upload

北城余情 提交于 2019-12-08 06:02:23
问题 I am using the following codes to upload files. File would be sent through superagent in blob format, convert it to dataURI when reaches server and save into AWS S3. files.forEach((file) => { superagent.post('http://localhost:3030/uploads') .attach('file', file) .on('progress', e => { console.log('Percentage done: ', e); }) .end((err, res) => { // console.log(err); console.log(res); }); }); File upload works but the progress bar is not populated correctly. As per the screenshot, you can see

How to send data correct axios Error: Multipart: Boundary not found

点点圈 提交于 2019-12-08 05:31:50
问题 I don't know why I receive on server [Error: Multipart: Boundary not found] and bundle.js:37628 POST http://localhost:8800/exporttocsv 500 (Internal Server Error ) When I make post through <form action="/exporttocsv" method="POST" encType="multipart/form-data"> post works correctly, but through axios doesn't work. Please help me fix the mistake this my code /--client import axios from 'axios' var formData = new FormData() const config = { headers: { 'Content-Type': 'multipart/form-data' } };

Annotation CrossOrigin not working in Spring boot

折月煮酒 提交于 2019-12-08 05:09:43
问题 I have a Spring Boot application that exposes some endpoints. From a React app I want to make requests to these endpoints, but it keeps giving me CORS problem: access to XMLHttpRequest at 'localhost:9090/helios-admin/api/dashboard/clients?page=0&size=30' from origin 'http://localhost:3000' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. So I've tried using @CrossOrigin annotation for all my methods

Django Rest Framework CORS blocking XMLHttpRequest

删除回忆录丶 提交于 2019-12-08 04:42:28
I have set up my CORS policy using Django-cors-headers with the following settings: APPEND_SLASH=False CORS_ORIGIN_ALLOW_ALL = True CORS_ALLOW_CREDENTIALS = True CORS_ORIGIN_WHITELIST = ( 'localhost:8000', 'localhost:3000', 'localhost' ) I have also added it to installed_apps and middleware. Now I am making a React app for the front end and using AXIOS for my API requests. When I make an API request to log in to my app the CORS policy allows it. But, if I make an API request that requires a Token, I get: Access to XMLHttpRequest at 'localhost:8000/api/TestConnection/' from origin ' http:/