Cross-origin ajax solution (recommended) during vue-cli development, vue-cliajax

Source: Internet
Author: User

Cross-origin ajax solution (recommended) during vue-cli development, vue-cliajax

Objective: To access the background interface to obtain data for a project built using vue-cli during development, a cross-origin problem occurs.

Configure the following in config/index. js:

[That is, when an ajax request is made, any request address starting with/api in the address is parsed as the target address, and target is the background interface address you want]

proxyTable: { ‘/api': { target: ‘https://188.188.18.8‘, changeOrigin: true, pathRewrite: { ‘^/api': ” } } }“`

Vue-resource call example

This. $ http. get ('/api/v4/user/login', [options]). then (function (response) {// callback for successful response}, function (response) {// callback for Error response });

Axios call example

 axios({  method: 'get',  headers: {'Accept': '*/*'},  url: '/api/v4/user/login',  data: options }) .then(function (response) {  console.log(response.data) }) .catch(function (error) {  console.log(error) })

Principles:

In configuration: target: 'https: // 188.188.18.8'

In the preceding vue-resource example, the first parameter is/api/v4/user/login.

The local server will automatically resolve it to https: // 188.188.18.8/v4/user/login, and this address is officially required.

Cross-origin principle (local files pretend to be on a remote server ):

Open the page in a browser. When a request is initiated: the address of the local server (usually localhost: 8080 or 127.0.0.1: 8080), the request is received, the request address contains a string or api. Then, the local server changes the request address to https: // 188.188.18.8/v4/(configure the address) + user/login (detailed address of the call method ).

At the same time, the address of the local server will change from localhost: 8080 to https: // 188.188.18.8/v4/. The result is:

Our local files are considered to be placed on the target address (https: // 188.188.18.8/v4/) server, and the files on the current server are requested for other things on the server, naturally, it is not cross-origin.

During the above vue-cli development, the ajax cross-origin solution (recommended) is all the content that I have shared with you. I hope to give you a reference, we also hope that you can support the customer's home.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.