【发布时间】:2019-09-09 05:18:39
【问题描述】:
我正在尝试使用 vuex + axios 从我的 API 中获取一些数据,但该操作给了我一个“网络错误”(ERR_BLOCKED_BY_CLIENT)。
当我使用 json-server 时,它运行良好,但即使使用 'Allow-Access-Control-Origin': '*'
,它也无法与我的 API 一起使用行动
const actions = {
async fetchSearch({ commit, state }) {
let res
try {
res = await axios(`http://localhost:8000/api/advertisements/search?column=title&per_page=${state.params.per_page}&search_input=${state.params.query.toLowerCase()}&page=${state.params.page}`, {
method: 'GET',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json'
}
})
} catch(err) {
console.log(err)
}
commit('clearProducts')
commit('setProducts', res.data)
},
setGlobalParams({ commit }, obj) {
commit('clearParams')
commit('setParams', obj)
}
}
组件
<script>
/* Vuex import */
import { mapActions } from 'vuex'
export default {
name: 'base-search-component',
data() {
return {
query_obj: {
page: 1,
per_page: 8,
query: ''
}
}
},
methods: {
...mapActions([
'fetchSearch',
'setGlobalParams'
]),
fetchData() {
if (this.query_obj.query === '') {
return
} else {
this.setGlobalParams(this.query_obj)
this.fetchSearch()
this.$router.push({ name: 'search', params: { query_obj: this.query_obj } })
}
}
}
}
</script>
【问题讨论】:
-
Access-Control-Allow-Origin是 response 标头,而不是 request 标头。您的 API 需要发送它。 -
@ceejayoz 现在我的服务器正在发送标头,但我无法在存储操作中获取数据。
标签: laravel vue.js axios vuex vue-router