• Welcome to the eight blog of that year. I am very glad to meet you at the right age.
  • The little buddy who has logged on QQ due to the theme shows the default head in the comments. Please go to the personal center to upload the avatar again.

Remember Vue.js uses Axios for cross domain requests.

Code Notes Barben 3 months ago (05-19) 152 browse 0 comments

Today is another day of fishing at home. Knock BUG Write an online translation function.

And then he took it down. Baidu translate open platform technical document In the example interface address, try and ask for normal request.

It's really sweet to request with Axios.

 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 a more fun request. No Access-Control-Allow-Origin header is present on the requested resource I will never forget to hit your head!

Obviously, this is due to the failure of requests across domains.

Resolving 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 shown below.

 ProxyTable: {
'/api': {
target:'https://www.barben.cn', / / write the domain name and port to call the interface, remember to add http. 
changeOrigin: true, 
pathRewrite: {
'^/api': / / / here is understood to replace /api in target address, calling the interface directly with /api instead. For example, if I want to call the https://www.barben.cn/request/ts interface, I can write /api/request/ts directly. I am very sorry.

4. restart the whole project.

After modifying proxyTable, the cross domain request is successfully solved.

Eight this year's blog, we've been there.
If the author does not indicate the original article, please indicate the link and source.
Remember the way Vue.js uses Axios for cross domain requests - https://www.barben.cn/code/482.html
Love ( Seven )

You must Sign in To comment!