Vue.js Learning cross-domain request agent and Axios: cross-domain Request proxy
1: Open Config/index.js
module.exports{ Dev: {
}}
Find proxytable{} here, instead:
Proxytable: { '/api ': { target: ' http://121.41.130.58:9090 ',//Set the interface domain name and port number you are calling do not forget to add the HTTP changeorigin: True, pathrewrite: { ' ^/api ': '//Here is understood to use '/api ' instead of the address inside the target, and in the later component we drop the interface directly with the API instead of such as I want to call ' http:// 40.00.100.100:3002/user/add ', directly write '/api/user/add ' can } } }
2: Use this in the component that needs to tune the interface:
Axios.post ('/api/yt_api/login/dologin ', postdata). Then (function (response) {Console.log (1) console.log (response);}). catch (function (error) {Console.log (error);})
Note: The original interface: Http://http://121.41.130.58:9090/yt_api/login/doLogin
Page Call: http://localhost:8081/api/yt_api/login/dologin-- There's a/api/here. Not superfluous, do not delete
Two:Axios Pass
1:vue official recommended component Axios the default is to commit the JSON string, so we're going to use the JSON string directly in the form behind the URL, directly to the JSON object in the line.
Let PostData = {' Companycode ': ' Tur ', ' userName ': ' 123456789123456789 ', ' Password ': ' 123456 '}axios.post ('/api/yt_api/' Login/dologin ', postdata). Then (function (response) {Console.log (1) console.log (response);}). catch (function (error) {Console.log (error);});
Here we will postdata this JSON object into the Post method, in the form of the page:
2: In the form of Key:val
Let PostData = Qs.stringify ({companycode: ' tur ', UserName: ' 123456789123456789 ', Password: ' 123456 '})
We need to operate on this JSON object, here's the QS you need to install first
NPM Install QS
Re-Import
Import qs from ' QS '
The form in the polygon is:
Cross-domain request agent and Axios of Vue.js learning