axios

How to take a jest snapshot after axios fetched data in componentDidMount?

心已入冬 提交于 2019-12-07 14:09:23
问题 Component to test class Carousel extends React.Component { state = { slides: null } componentDidMount = () => { axios.get("https://s3.amazonaws.com/rainfo/slider/data.json").then(res => { this.setState({ slides: res.data }) }) } render() { if (!slides) { return null } return ( <div className="slick-carousel"> ... markup trancated for bravity </div> ) } } export default Carousel Test import React from "react" import renderer from "react-test-renderer" import axios from "axios" import Carousel

【HAVENT原创】让 axios 支持 jsonp

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 12:42:51
因为不想让再引用新的第三方组件了,所以执念了一下,于是搜索到了下面的代码 //axios本版本不支持jsonp 自己拓展一个 axios.jsonp = (url) => { if(!url){ console.error('Axios.JSONP 至少需要一个url参数!') return; } return new Promise((resolve,reject) => { window.jsonCallBack =(result) => { resolve(result) } var JSONP=document.createElement("script"); JSONP.type="text/javascript"; JSONP.src=`${url}&callback=jsonCallBack`; document.getElementsByTagName("head")[0].appendChild(JSONP); setTimeout(() => { document.getElementsByTagName("head")[0].removeChild(JSONP) },500) }) } 调试了一下,发现确实能用,但是存在一个缺陷,就是如果存在连续多次的请求,都会回调到同一个函数上,这样会导致获取异步结果的时候只返回了最后一次的结果,于是乎调整了一下函数

springmvc获取axios数据为null情况

混江龙づ霸主 提交于 2019-12-07 10:53:55
场景:前端用了vue没有用ajax与后台通信,用了axios,但是在代码运行过程中发现axios传递到后台的值接受到数据为null。 问题原因:此处的问题在与axios返回给后台的数据为json类型的,后台接收和返回也应该是json类型的才行。 解决方式:这里用的是@ResquestBody和@ReponseBody这两个注解来解决的。如下图代码使用@RequestBody Map map去获取前台传递过来的数据,然后返回给前端的数据也必须为json格式,这时候需要在方法上方加一个@ReponseBody注解。或者在类的上用@RestController代替@Controller注解也有相同的效果。 public String toMainPage(@RequestBody Map map, HttpServletRequest request) throws Exception { String username = (String) map.get("username"); String password = (String) map.get("password"); String flag = (String) map.get("flag"); ModelAndView mv = new ModelAndView(); HttpSession session =

Axios: how to cancel request inside request interceptor properly?

混江龙づ霸主 提交于 2019-12-07 07:06:59
问题 I want to cancel the request if there's no token so I do like this: instance.interceptors.request.use(config => { if (!getToken()) { console.log("interceptors: no access token"); } else { config.headers.Authorization = "Bearer " + getToken().accessToken; return config; } }); But in negative scenario there's an error TypeError: Cannot read property 'cancelToken' of undefined . 回答1: You cannot use the token inside the interceptors but instead throw Cancel axios.interceptors.response.use

post request using axios on Laravel 5.5

拟墨画扇 提交于 2019-12-07 06:44:48
问题 i'm trying to make some requests using axios and the last Laravel version 5.5 after configure the X-CSRF fields and all my code is simple : axios.post('/post-contact',{name:'Kamal Abounaim'}) .then((response)=>{ console.log(response) }).catch((error)=>{ console.log(error.response.data) }) but i get this error : 419 (unknown status) what the problem supposed to be Thanks for answering 回答1: This is happening because of the csrf-token. Just add meta tag with the csrf-token in the <head> and add

Cannot Basic Auth from React App with Axios or SuperAgent

谁说胖子不能爱 提交于 2019-12-07 05:55:37
问题 I try to make a GET request with axios and I always get 401. This happens only when I send the request from my react app. axios.get('http://localhost:8080/vehicles', { withCredentials: true, auth: { username: 'admin', password: 'admin' }, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, }) Postman GET request with the same credentials and Basic Auth set, works. If I simply type in my browser the url, a login box appears and with the same credentials, it works, I

Mock axios with axios-mock-adapter get undefined resp

荒凉一梦 提交于 2019-12-07 04:29:13
问题 I created an axios instance ... // api/index.js const api = axios.create({ baseURL: '/api/', timeout: 2500, headers: { Accept: 'application/json' }, }); export default api; And severals modules use it .. // api/versions.js import api from './api'; export function getVersions() { return api.get('/versions'); } I try to test like .. // Test import { getVersions } from './api/versions'; const versions= [{ id: 1, desc: 'v1' }, { id: 2, desc: 'v2' }]; mockAdapter.onGet('/versions').reply(200,

React testing onSubmit using axios

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 02:59:27
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': 'application/json' }, cache : false }) .then(function (response) { //alert("Form Submitted."); this

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

青春壹個敷衍的年華 提交于 2019-12-07 01:14:30
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' } }; export const ipmortToCSV = (file) => dispatch => { formData.append('file',file) console.log(formData

Error: getaddrinfo ENOTFOUND

走远了吗. 提交于 2019-12-07 00:28:41
问题 I have a simple Node.js bot that makes an HTTP request each second to a rest API. If the returned data is right then I construct an URL where I HTTP POST. Everything works alright but after ~4-5hrs of running I got this error 0|server | error: Error: getaddrinfo ENOTFOUND www.rest-api.com www.rest-api.com:443 0|server | at errnoException (dns.js:28:10) 0|server | at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:73:26) Can someone explain to me why this has happened? After I restart my