我想在Vue.js中调用多个API,如下所示:
<script>
const app = new Vue({
el: '#app',
data: {
array1: [],
array2: []
},
created(){
fetch('http://localhost/api/first')
.then(Response => Response.json())
.then(json => {
this.array1 = json
}),
fetch('http://localhost/api/second')
.then(Response => Response.json())
.then(json => {
this.array2 = json
})
}
})
</script>当我在Chrome中使用Vue devtools时,我可以看到这两个数组是被识别的,当我使用Fiddler时,我可以看到这两个API都被调用。问题是第二组JSON结果,即来自http://localhost/api/second的JSON不填充array2。
有什么想法吗?
发布于 2018-05-10 10:36:53
只是为了回答我自己的问题以防其他人遇到类似的问题。首先,上述问题中的代码工作得很好。我的问题是在服务器端没有为http://localhost/api/second API启用CORS,因此需要启用CORS。Fiddler不受CORS限制的影响,因此可以很好地获取API结果。
https://stackoverflow.com/questions/50269289
复制相似问题