Vue 跨越问题解决 No ‘Access

编程入门 行业动态 更新时间:2024-10-22 14:35:08

<a href=https://www.elefans.com/category/jswz/34/1770550.html style=Vue 跨越问题解决 No ‘Access"/>

Vue 跨越问题解决 No ‘Access

一、问题

Access to XMLHttpRequest at 'http://xxx/wx_pn/wx_code_ticket' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

二、解决方法

  1. 项目根目录下创建 vue.config.js
module.exports = {devServer: {open: true,host: 'localhost',port: 项目运行端口,https: false,hotOnly: false,proxy: {// 配置跨域'/api': {target: 'http://xxx',//后端接口地址ws: true,changOrigin: true,pathRewrite: {'^/api': ''}}},}}
  1. 配置main.js
import Vue from 'vue'
import axios from 'axios'
...Vue.prototype.$axios = axios
axios.defaults.baseURL = '/api'...
  1. 使用方式

方式一

// 微信公众号请求
import axios from 'axios'
// 超时时间
axios.defaults.timeout = 10 * 1000
// 跨域请求允许保存cookie
axios.defaults.withCredentials = truelet base = '/aaa'// 获取ticket
export const wx_code_ticket = () => {return axios({url:`${base}/bbb`,method:'GET',})
}
<script>
import {wx_code_ticket} from '@/api/wx_pn'
export default {name:'Code',data() {return {img_base64:'',}},beforeMount() {this.getCode()},methods:{getCode:()=>{return new Promise(()=>{console.log(wx_code_ticket)wx_code_ticket().then(resp=>{console.log(resp)})})}}
}
</script>

方式二

```js
<script>
export default {name:'Code',data() {return {img_base64:'',}},beforeMount() {this.$axios({url:`/aaa/bbb`,method:'GET',}).then(resp=>{console.log(resp)})},
}
</script>

参考:
.html

(原理)

更多推荐

Vue 跨越问题解决 No ‘Access

本文发布于:2024-03-07 20:12:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1718808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Vue   Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!