Problem Description:
The project created using VUE-CLI, the development address is localhost:8080, needs to access the interface on the localhost:8888.
Analysis Reason:
Access between different domain names requires Cross-domain to be properly requested. There are many ways to cross domains, usually requiring background configuration.However, the project created by VUE-CLI can use the Node.js Proxy server directly to implement Cross-domain requests.
automatically determine whether the current development or production environment, and then automatically match the Api_host, we can use the Process.env.API_HOST in any component of the address such asInstance.post (process.env.api_host+'user/login'this. form)Then the second step back-end server configuration Cros cross-domain, that is, access-control-allow-origin:* allows all access to the meaning.Comprehensive: Under the environment of development we can configure a
In the Vue project, the list data needs to use the QQ music interface data, but the direct request does not have the host and the referer limit, needs to adopt the way of the back-end proxy. With Axios and Node Express, after implementation in Dev-server.js, restart the project, resulting in interface 404. The code is as follows: After careful examination, the code is no problem, so in apirouts Console.log
authentication should be connected to ' proxy ' and provide a certificate.This will set a ' Proxy-authorization ' header (header) to overwrite the original custom.proxy:{host:127.0.0.1,port:9000,auth:{Username: ' CDD ',Password: ' 123456 '}},' Canceltaken ' defines a cancellation that can be used to cancel a request(See the detailed section of the cancellation below)Canceltoke:new Canceltoken (function (ca
(typeof XMLHttpRequest!== ' undefined ') {For browsers use XHR adapteradapter = require ('./adapters/xhr ');} else if (typeof process!== ' undefined ') {For node use HTTP adapteradapter = require ('./adapters/http ');}return adapter;If the browser is executed XHR, does not support proxy, if run in Nodejs, with HTTP adapter, support proxy, so proxy is the functio
Because only the get and post methods can be used in Axios to make request data cross-domain Access data without JSONP and so on if you want to use Axios to do cross-domain access directly it is not possible to configure the agent why do I need to configure the agent? The reason is because the client is requesting that the data on the server has cross-domain problems and that the servers and servers can req
Detailed explanation of dynamic Axios configuration steps and axios steps
Preface
Previously, Vue-resource was used as the project ajax library when I was writing a vue project. One day in July, especially the updates on Weibo showed that the ajax library should be generic, the technical support for vue-resource is abandoned, and axios is recommended.
We recommen
Reprint: https://www.cnblogs.com/libin-1/p/6607945.htmlIntroduction Method:$ npm Install axios$ cnpm Install Axios//taobao Source $ bower Install Axios or use CDN: src= "Https://unpkg.com/axios/dist/axios.min.js">script >
Give me a chestnut: Execute GET request//Make a request to a user with the specified ID Th
This article brings you the content is about Vue based on the Vuex and Axios interceptors to achieve loading effect and Axios installation configuration, there is a certain reference value, the need for friends can refer to, I hope to help you.
Get ready
Creating Projects with VUE-CLI scaffolding
Enter Project installation Vuex, Axios (npm install vuex,
Sometimes need to write a long path, annoying, so you can write the same operation together, if you need to modify, you can modify the Axios request
Like BaseURL: ' https://www.baidu.com ',
1. Configure Config/index.js: Troubleshoot cross-domain issues dev: {env:require ('./dev.env '), port:8008, Autoopenbrowser:false, Assetssubdirector Y: ' Static ', Assetspublicpath: '/', proxytable: {'/ajaxurl ': {target: ' https://www.aaaaaaaa.com/', change Origi
' header set by using ' header '. Proxy: {host: ' 127.0.0.1 ', Port: 9000, Auth:: {username: ' mikeymike ', Password: ' rapunz3l '}}, //' Canceltoken ' Specifies the Cancel token //for canceling the request (see later cancellation this section learn more) Canceltoken: new canceltoken (function
Additional notes:XMLHttpRequest or fetch is a browser-enabled feature, and Axios is the
Based on the axios encapsulation fetch method and call instance, the axios encapsulation fetch instance
For basic axios usage, see the official axios website.
// Dependent on axios to modify private ajax import Qs from 'qs' import axios
Axios global request parameter settings, request and return interceptor methods, axios global
Application scenarios:
1. parameters included in each request, such as tokens and timestamps.
2. Determine the returned status, such as whether the token has expired.
The Code is as follows:
Axios. interceptors. request. use (config => {var xtoken = getXtoken () if
Front-end project is done with Vue.js, front-end service URL:http://localhost:8080/Back-end projects are made with node. js, backend service URL:http://localhost:3000/Now the front end is going to call back-end service one of the interfaces, URL:http://localhost:3000/goods, this interface requires 3 parameters (page,pagesize,sort)There are two ways to invoke the back-end interface:1) Call the Axios request back-end service directlyIf you use Axos to i
Workaround 1: (There is a problem with iOS compatibility, it is not recommended)// the JSON format is converted to Formdata format because of the reasons for some interfaces function Json2formdata (jsondata) { varnew urlsearchparams (); for (var in Jsondata) { // traverse each key/value of the JSON object to params.append (Key, Jsondata[key]) ; } return params;}Workaround 2: Use node's QS module, recommendedImport Axios from '
Vue Advanced (iii): Axios use detailed
HTTP request clients based on Promise, which can be used in both browsers and Node.js. functional features send xmlhttprequests requests in browsers, HTTP requests in Node.js, support Promise APIs, intercept requests and responses, transform request and response data, convert JSON data automatically, and client support protection Ann All free from XSRF attacks; browser support; Install
Use Bower:
$ Bower Install
a development environment, the interface we request needs to be configured if there are cross-domain issuesThere is a parameter called proxytable in the index.js in the VUE-CLI config file.When configuring Proxytable, the local virtual server will receive your request and send it on your behalf, so there will be no cross-domain issues, of course, this applies only to the development environment.Specific configuration code:Configure agentsProxytable: {'/api ': {//API is a matchTarget: ' http://w
a development environment, the interface we request needs to be configured if there are cross-domain issuesThere is a parameter called proxytable in the index.js in the VUE-CLI config file.When configuring Proxytable, the local virtual server will receive your request and send it on your behalf, so there will be no cross-domain issues, of course, this applies only to the development environment.Specific configuration code:Configure agentsProxytable: {'/api ': {//API is a matchTarget: ' http://w
Using Axios in Vue to implement cross-domain requestsDemand analysis: In the project needs to crawl QQ music song list of data, due to request the data address URL=HTTPS://C.Y.QQ.COM/SPLCLOUD/FCGI-BIN/FCG_GET_DISS_BY_TAG.FCG. From the official website of QQ music you can see that the domain name in Referer in the request header is y.qq.com (the domain name of the sending request page), and the host domain name is c.y.qq.com (the domain name of the req
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.