Front end cross domain solutions


What is cross domain?

  • Cross domain is a restriction policy made by browser for security
  • Browser requests must follow the same origin policy: same domain name, same port and same protocol

How to solve cross domain problems?

There are three ways to solve cross domain problems

  • CORS cross domain
  • Jsonp cross domain
  • Agent cross domain
CORS cross domain:

CORS cross domain front-end does not need to be set, the back-end can directly add the previous code

The back-end is directly set to allow access to a domain name


Or allow access to all domain names

Jsonp cross domain:

Jsonp cross domain requires both front-end and back-end settings

front end:

Install the jsonp plug-in

npm i jsonp

Import the file that needs to be jsonp requested

import jsonp from 'jsonp'
export default {
  mounted() {
    let url = "http://localhost/zuoye/jsonp.php";

Back end:

//The server returns JSON data  
//Dynamic execution of callback function  
echo $callback."($result)"; 

Then we can open the console and see the data.

Agent cross domain

The front end modifies the nginx server configuration, but the back end does not move.

For example, our local service is, and we want to request http: / / interface

stay vue.config.js Inside:

module.exports = {
    devServer: {
        host: 'localhost',
        port: 8080,
        proxy: {
            '/api': {
                Target: "http: //", <! -- requested interface -- >
                changeOrigin: true,
                pathRewrite: {
                    '/api': ''

On the requested page:


At this point, although the request is sent to: http://localhost : 8080 / API / GetData /, but it has been proxy to http: / / /