Vue中如何利用Ajax实现跨域请求

来源:互联网转载和整理 发布时间:2025-08-17

ajax跨域请求

一.设置config/index.js || proxyTable添加

proxyTable:{'/api':{target:'http://192.168.48.239:8080/ydzl',changeOrigin:true,pathRewrite:{'^/api':''}}

二.mian.js 添加vue全局属性

Vue.prototype.HOST='/api'

三.如果是post的话

1.修改数据格式

transformRequest:[function(data){//Dowhateveryouwanttotransformthedataletret=''for(letitindata){ret+=encodeURIComponent(it)+'='+encodeURIComponent(data[it])+'&'}returnret}],

2.修改请求头

headers:{'Content-Type':'application/x-www-form-urlencoded'}
热门标签