简体   繁体   English

Vue.js,对Laravel API的Axios多个get CORS请求随机失败

[英]Vue.js, Axios multiple get CORS requests to Laravel API are randomly failing

I have this problem - on Vue component created life cycle hook I am making 2 CORS get requests via Axios to external Laravel API. 我有这个问题-在Vue组件创建的生命周期挂钩上,我通过Axios向外部Laravel API发出了2个CORS获取请求。 If I make only one of the requests everything is fine and it works 100% of the time. 如果我只提出其中一项要求,那么一切都很好,并且100%的时间都有效。 But if I make 2 or more requests I sometimes get failed requests on random in the network tab. 但是,如果我发出2个或更多请求,有时我会在网络标签中随机收到失败的请求。 Obviously I am doing something wrong with Axios. 显然我在使用Axios做错了。 Can you please help me. 你能帮我么。

This is my component created hook - I call VueX actions. 这是我的组件创建的挂钩-我称之为VueX操作。

 created () { this.$store.dispatch('getPets'); this.$store.dispatch('getSpecies'); }, 

And this are my actions in VueX store 这是我在VueX商店中的动作

 actions: { getPets(context) { return new Promise((resolve, reject) => { axios.get('api/pets') .then(response => { context.commit('SET_PETS', response.data); context.commit('SET_SELECTED_PET', response.data.data[0]); resolve(response); }) .catch(error => { reject(error); }); }); }, getSpecies(context) { return new Promise((resolve, reject) => { axios.get('api/species') .then(response => { context.commit('SET_SPECIES', response.data); resolve(response); }) .catch(error => { reject(error); }); }); }, setSelectedPet(context, pet) { context.commit('SET_SELECTED_PET', pet); }, } 

Then I get failed requests on random - sometimes both requests are ok (200 status), other times one of them is failing... Requests 然后,我随机收到失败的请求-有时两个请求都可以(200状态),而有时其中一个失败... 请求

The request is failed - there is no response, I think that the request does not go to the Laravel API at all. 该请求失败-没有响应,我认为该请求根本不会转到Laravel API。 Laravel logs are empty too. Laravel日志也是空的。

I think I am doing something wrong with Axios, because its not from my browser or firewall - I have stopped firewall and tested in incognito and other browsers without any extensions. 我认为Axios出了点问题,因为它不是从我的浏览器或防火墙发出的-我已经停止了防火墙,并在无任何扩展的隐身和其他浏览器中进行了测试。 Any help will be appreciated. 任何帮助将不胜感激。

This are the Axios headers I set up in the main js file. 这是我在主要js文件中设置的Axios标头。

 // Set axios to call the backend API and set its headers on every page reload window.axios = require('axios'); window.axios.defaults.baseURL = 'http://api.aaa'; window.axios.defaults.timeout = 30000; window.axios.defaults.headers.common = { 'X-Requested-With': 'XMLHttpRequest', 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'Bearer '+getToken(), }; 

Ok, after some time I finally figured it out. 好的,过了一段时间我终于想通了。 It was the PHP opCache that was causing this behavior. 导致此行为的是PHP opCache。 If you have the similar problem just turn opChache off. 如果您遇到类似的问题,请关闭opChache。

Btw can you give me your thoughts about what can cause this behavior from Laravel because I dont want to lose opCache as an option for similar Laravel projects? 顺便说一句,您能给我我的想法是什么会导致Laravel出现这种情况,因为我不想失去opCache作为类似Laravel项目的选择吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM