axios 跨域配置 调用ashx
要在axios中跨域调用ashx文件,需要在axios中配置proxy。以下是一个简单的示例:
// 在vue项目中的vue.config.js文件中配置
module.exports = {
devServer: {
proxy: {
'/api': {
target: 'http://localhost:8080', // ashx文件所在的域名和端口
changeOrigin: true,
pathRewrite: {
'^/api': '/path/to/ashx' // ashx文件的路径
}
}
}
}
}
然后,在你的代码中使用axios调用ashx文件:
import axios from 'axios';
axios.get('/api').then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
这样配置后,axios会自动将以/api开头的请求代理到http://localhost:8080/path/to/ashx,实现跨域调用ashx文件
原文地址: http://www.cveoy.top/t/topic/hQUR 著作权归作者所有。请勿转载和采集!