• Welcome to Baben's blog that year. I'm glad to meet you at the right age!
  • Due to the theme, the QQ login partner displays the default avatar in the comments. Please go to the personal center to upload the avatar again.

Record Vue.js's method of cross domain request using Axios

Code Notes barben Five years ago (2019-05-19) 48056 views 2 comments

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

Then it was taken down Baidu Translate Open Platform Technical Document See the example interface address in.

Use axios to request, really fragrant

 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 that the request for more fun, this No Access-Control-Allow-Origin header is present on the requested resource I will never forget hitting your head!

Obviously, this is the request failure caused by 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 the domain name and port of the interface to be called, and remember to add http. changeOrigin: true, pathRewrite: { '^/api': ''//It is understood here that/api is used to replace the address in the target, and/api is used directly when calling the interface. For example, I want to call https://www.barben.cn/request/ts Interface, just write/api/request/ts directly. } } }

4. Restart the entire project

After modifying the proxyTable, the problem of cross domain requests was successfully solved



Eight blogs that year, we have been
If the author does not indicate that it is an original article, please indicate the link and source of this article for reprinting
Record Vue.js's method of cross domain request using Axios- https://www.barben.cn/code/482.html
Like( eleven )
Post my comments
Cancel comment
expression Mapping Bold Strike through Center Italic

You need to bring your nickname and email with you in the review of Eight Books that year!

  • Nickname (required)
  • Email (required)
  • website
Successfully captured two Only rare elves
  1. Great, clear and clear. It solved my cross domain problem in an instant
    scofield1906 2021-02-19 17:29 reply
  2. Boss, I want to learn the special effects made by canvas. There is a girl beside the website who will follow the mouse and click on her to make various movements. Can you write a tutorial? Thank you very much for your technical breakthrough
    Front white_summer 2019-12-03 17:52 reply