Vue+axios cross-Domain resolution method

Source: Internet
Author: User

Cross-domain issues can also be addressed through this approach.

Use Http-proxy-middleware Agent solution (project using VUE-CLI scaffolding construction)

For example, the requested URL: "Http://f.apiplus.cn/bj11x5.json"

1. Open Config/index.js, add the following code in Proxytable:

123456789 proxytable: {     '/api ': { //use "/API" instead of "HTTP://F.APIPLUS.C"        target: '/HTTP/ F.apiplus.cn ',//source address        changeorigin:true,//Change source        pathrewrite: {          ' ^/api ': ' http://f.apiplus.cn '//path rewrite          }     }  

2. Use "/API" directly when requesting data using Axios:

1234 getData () {  axios.get(‘/api/bj11x5.json‘, function (res) {    console.log(res)  })

This approach solves the cross-domain, and when you package the deployment, there is a problem. Here's how to fix it:

12345 let serverUrl = ‘/api/‘  //本地调试时 // let serverUrl = ‘http://f.apiplus.cn/‘  //打包部署上线时 export default {   dataUrl: serverUrl + ‘bj11x5.json‘ }
When debugging, define a ServerURL to replace our "/API", finally packaging, only need to "http://www.xxx.com" replace the "/api" on it. Original website: https://www.cnblogs.com/wangyongcun/p/7665687.html

Vue+axios cross-Domain resolution method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.