• Welcome to the eight year blog, I'm glad to meet you at the right age!
  • Due to the theme, QQ login partners will display the default avatar in the comments. Please go to the personal center and upload the avatar again.

Remember how vue.js uses Axios to make cross domain requests

Code Notes barben One year ago (May 19, 2019) 1590 views 1 comment

Today is another day to fish at home Knock a bug Write an online translation function.

And then it was removed Baidu translation open platform technical documents In the example interface address, try to see if the normal request down.

Request with Axios. It's delicious

 axios.get('http://api.fanyi.baidu.com/api/trans/vip/translate?q=apple&from=en&to=zh&appid=2015063000000001&salt=1435660288&sign=f89f9594663708c1605f3d736d01d2d4')
.then((response) => {
    console.log(response);
})
.catch((error) => {
    console.log(error);
})

This story tells us, again amusing request, this No Access-Control-Allow-Origin header is present on the requested resource I will never forget to hit your head!

Obviously, this is a request failure due to cross domain.

Resolve cross domain requests

1. Open the config / index.js file
2. Find the following code in index.js

 module.exports = {
  dev: {
    proxyTable: {}
  }
}

3. Modify the proxytable in the code as follows

 Proxytable: {
 '/ API': {target: 'HTTPS: / / www.barben. CN', / / write down the domain name and port of the interface to be called, and remember to add http. changeorigin: true, 
, pathrewrite: {
 '^ / API': '/ / here, / API is used to replace the address in target, and / API is directly used when calling the interface. For example, if I want to call the interface of https://www.barben.cn/request/ts, I can directly write / API / request / ts. 
    }
  }
}

4. Restart the whole project

After modifying the proxytable, the problem of cross domain request is successfully solved



Eight blogs that year, we've been there all the time
If the author does not indicate the original article, please indicate the link and source of this article
Remember vue.js uses Axios to make cross domain requests - https://www.barben.cn/code/482.html
Like it( ten )
Post my comments
Cancel comment
expression Mapping Bold Strikethrough Center Italics

You need to bring your nickname and email with you in the year of eight!

  • Nickname (required)
  • Email (required)
  • website
Successfully captured one A rare spirit
  1. Thank you very much for the special effects you can do on the website
    Front end Xiaobai_ summer 2019-12-03 17:52 reply