axios

Axios - How to read JSON response?

那年仲夏 提交于 2019-12-04 00:55:30
Axios 0.17.1 .then(function (response) { console.log(response); //console.log(response.status); //It is an error -> SyntaxError: Unexpected token u in JSON at position 0 console.log(JSON.parse(response.data.error)); console.log(response.data.error); //undefined. The console.log of response is {data: "{"error":"Name must be entered with more than one … NULL↵ ["isPipe":protected]=>↵ NULL↵ }↵}↵", status: 203, statusText: "Non-Authoritative Information", headers: {…}, config: {…}, …} config : {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}

React: Axios Network Error

江枫思渺然 提交于 2019-12-04 00:53:37
This is my first time using axios and I have encountered an error. axios.get( `http://someurl.com/page1?param1=1&param2=${param2_id}` ) .then(function(response) { alert(); }) .catch(function(error) { console.log(error); }); With the right url and parameters, when I check network requests I indeed get the right answer from my server, but when I open console I see that it didn't call the callback, but instead it caught an error. Error: Network Error Stack trace: createError@ http://localhost:3000/static/js/bundle.js:2188:15 handleError@ http://localhost:3000/static/js/bundle.js:1717:14 If

axios http always returns with empty data

南楼画角 提交于 2019-12-03 21:39:49
I asked this question before and wasn't able to get an answer. I was able to do use method: 'get' like below to get it working so it was okay but this time I need to use post. In a different project (using react, redux, php, webpack, xampp) the same issue has resurfaced and I am trying to figure it out. So here it is: register.php echo $_GET['task']; index.js const values = {task: 'doSomething', username: 'username'} axios({ url: "./server/register.php", timeout: 20000, method: 'get', params: values }).then(function(response){console.log(response.data)}) When I do the above everything is okay

Axios Post Form with Reactjs

馋奶兔 提交于 2019-12-03 20:38:23
So I have this post method with Axios and if I submit this, it said Uncaught (in promise) Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:87) If I use this method: axios.post('http://localhost:5000/users', ({userid: this.state.userid}) it works. But if I add 2 or more arg to the axios post it gets error again: axios.post('http://localhost:5000/users', ({userid: this.state.userid}, {fullname: this.state.fullname} )) Here is my full code. As you can see I try different combinations of code, and it only works if I only pass 1 arg. import React from

Using axios with async and await

情到浓时终转凉″ 提交于 2019-12-03 20:35:59
I am new in Async and await ecosystem, but I know that it gives the way of coding in a synchronous way (although it is async behind the scenes, just the way it is written in code). So here is my code that I want to do in an async fashion. const axios = require("axios"); async function getJSONAsync(){ // The await keyword saves us from having to write a .then() block. let json = await axios.get('https://tutorialzine.com/misc/files/example.json'); console.log('after the call to service'); // The result of the GET request is available in the json variable. // We return it just like in a regular

Axios catch error Request failed with status code 404

笑着哭i 提交于 2019-12-03 20:23:24
I'm testing a login component that uses Axios. I tried mocking Axios with axios-mock-adapter , but when I run the tests, it still errors out with: Error: Request failed with status code 404 How do I properly mock Axios in my tests? login.spec.js: import Vue from 'vue' import { shallowMount, createLocalVue } from '@vue/test-utils'; import Login from '../../src/components/global/login/Login.vue'; import Raven from "raven-js"; import jQuery from 'jquery' import Vuex from 'vuex' import router from '../../src/router' var axios = require('axios'); var MockAdapter = require('axios-mock-adapter');

Axios: Upload progress for multiple file uploads

好久不见. 提交于 2019-12-03 17:24:52
Following https://github.com/mzabriskie/axios/blob/master/examples/upload/index.html I've set up a file upload with progress bar. However, I have <input type="file" multiple> , so the upload is inside a loop like this: for (var i=0; i<files.length; i++) { var config = { onUploadProgress: function(progressEvent) { var what = Math.round( (progressEvent.loaded * 100) / progressEvent.total ); } }; axios.post(url, data, config) .then(function (response) { }); } The question is: How can I assign the upload progress (see var what ) to the corresponding file? Everything I've tried didn't work: The

Using JavaScript Axios/Fetch. Can you disable browser cache?

元气小坏坏 提交于 2019-12-03 16:44:19
I am trying to query a quote API for a freeCodeCamp project I'm updating to React.js. I am now trying to use Fetch or Axios to query the API but it's caching the response in the browser. I know in $ajax there is a { cache: false } that would force the browser to do a new request. Is there some way I will be able to do the same with Fetch or Axios ? The cache-control setting seems to be already set to max-age: 0 by Axios . This is my code I have that is querying the API. generateQuote = () => { axios.get('https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1')

Axios-Make multiple request at once (vue.js)

霸气de小男生 提交于 2019-12-03 08:12:39
How to make multiple requests in parallel using axios and vue ? roli roli Since axios can be used by React and Vue it is pretty match the same code. Make sure to read axios docs , you can understand it from there. Anyway, I am going to show you an example: <template> <div> <button @click="make_requests_handler">Make multiple request</button> {{message}} - {{first_request}} - {{second_request}} </div> </template> And the script: import axios from 'axios' export default { data: () => ({ message: 'no message', first_request: 'no request', second_request: 'no request' }), methods: { make_requests

How can you use axios interceptors?

◇◆丶佛笑我妖孽 提交于 2019-12-03 07:48:00
问题 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