axios

javaWeb项目跨域问题解决方案:

我只是一个虾纸丫 提交于 2019-12-02 06:43:30
1.简单的servlet项目 1.配置一个filter过滤器,过滤所有的请求,并且设置响应头 package Filter; import javax.servlet.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /*自定义拦截器 用于给每个都加上跨域的头*/ public class CORSFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //变成http的 HttpServletResponse resp = (HttpServletResponse) response; // 添加参数,允许任意domain访问 resp.setContentType("text/html;charset=UTF-8"); //禁用缓存,确保网页信息是最新数据 resp.setHeader("Pragma","No-cache"); resp.setHeader("Cache-Control","no

Load data before createStore

不打扰是莪最后的温柔 提交于 2019-12-02 05:43:05
I’ve created some React files where one initializes a Redux store. However, I really need to load some data from a json file before store is initialized. I’ve tried to import a script loading the json structure then assigning it to the createStore initialState value. But createStore runs before the data is loaded and assigned. Is there any simple way to say “dont do anything before my axios call is done”??? Action types actiontypes.js export const LOAD_DATA_REQUEST='LOAD_DATA_REQUEST'; export const LOAD_DATA_SUCCESS='LOAD_DATA_SUCCESS'; export const LOAD_DATA_ERROR='LOAD_DATA_ERROR'; Actions

Send nested objects in GET method URL search params in Axios

久未见 提交于 2019-12-02 05:06:09
问题 I am trying to send the request with URL search params as below but I am not able to access a nested object filter on the server side. axios.get('/get handler', { params: { room: 1, filter: { fan: 2, table: 1, } }); What Am I possibly doing wrong? I am using Django restFramework 3 on the server side and I am not able to access filter key in the method. I am accessing query params using request.query_params but when i do request.query_params.get('filter') I get none 回答1: You need to serialize

axios transformRequest - how to alter JSON payload

孤人 提交于 2019-12-02 04:35:21
问题 I am using axios in my Express API and I want to transform the payload before sending it off to another API. axios has just the thing for this called transformRequest. This is where I ran into issues though. The code I have looks like: const instance = axios.create({ baseURL: 'api-url.com', transformRequest: [ (data, headers) => { const encryptedString = encryptPayload(JSON.stringify(data)); data = { SecretStuff: encryptedString, }; return data; }, ], }); // firing off my request using the

how to serialize parameters using axios

前提是你 提交于 2019-12-02 01:58:47
How would I serialize url parameters for use with axios ? My parameters are just numbers in array [1,2] . Here is my code so far axios({ method: 'delete', url: '/api/' }) My request url will be something like this http://127.0.0.1:8000/api/?id=1&id=2 I looked at the paramsSerializer method that axios has but its confusing how it can be used or whether its even appropriate in my case. Please advice. Thanks the config object of axios.get accepts params . In params you can specify your array and it will do the conversion for you. Here is an example: axios.get('/api/', { params: { id: [1,2] } })

axios transformRequest - how to alter JSON payload

牧云@^-^@ 提交于 2019-12-02 00:39:11
I am using axios in my Express API and I want to transform the payload before sending it off to another API. axios has just the thing for this called transformRequest . This is where I ran into issues though. The code I have looks like: const instance = axios.create({ baseURL: 'api-url.com', transformRequest: [ (data, headers) => { const encryptedString = encryptPayload(JSON.stringify(data)); data = { SecretStuff: encryptedString, }; return data; }, ], }); // firing off my request using the instance above: const postData = { id: 1, name: 'James', }; instance.post('/getStuff', postData) and

React - Controlling multiple Ajax Calls

强颜欢笑 提交于 2019-12-01 22:40:29
In my react application, I have a Grid. User can select many grid rows at a time and click on a button to take bulk action on selected grid rows. On a server side I have a script which I want to execute for each selected row (to make question simple I am making call to "jsonplaceholder.typicode.com" in the example below for each selected row) on a click of a bulk action button. On bulk action button click, I get selectedRows in action creator, where I iterate over selectedRows and make ajax call for each of the selected row. Since selectedRows may contain more than 1000 items and if I just

Chrome: Invoking 'alert()' during microtask execution is deprecated and will be removed

孤街浪徒 提交于 2019-12-01 21:57:41
While working on my web app, specifically file uploads, Chrome displayed a warning after I called an alert: Invoking 'alert()' during microtask execution is deprecated and will be removed in M53, around September 2016. See https://www.chromestatus.com/features/5647113010544640 for more details. However I think that in my situation the call is justified and am a little worried my code won't work once M53 is released. Note that I'm not shipping to production with the alerts, but for testing it is quite valuable. The situation: I am developing my app in typescript using react. I'm using axios to

POST Image which store as a Blob with Axios - VUEJS

自古美人都是妖i 提交于 2019-12-01 21:39:28
问题 I have a data Image which store as a Blob but I dont know how to post it with Axios, I use VUEJS. Please help me. My Object API by VueDevtool <file-upload v-model="files"></file-upload> <button type="submit" v-on:click.prevent="Submit">Submit</button> <script> methods: { data: function () { return { config: { 'headers': {'Authorization': 'JWT ' + this.$store.state.token}, 'Content-Type': 'multipart/form-data' } }, methods:{ for (var file in this.files) { let data = new FormData() data.append(

Axios onUploadProgress only fires once on my machine

隐身守侯 提交于 2019-12-01 21:33:42
If I use this fiddle https://jsfiddle.net/v70kou59/1/ everything works as expected (function () { var output = document.getElementById('output'); document.getElementById('upload').onclick = function () { var data = new FormData(); data.append('foo', 'bar'); data.append('file', document.getElementById('file').files[0]); var config = { onUploadProgress: function(progressEvent) { var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total ); console.log(percentCompleted) } }; axios.put('/upload/server', data, config) .then(function (res) { output.className = 'container';