Vue cross-domain processing issues (detailed tutorial)
This article gives you a detailed introduction to the way Vue handles cross-domain problems and an introduction to related knowledge points. Friends who are interested in this can learn from it.
Set express proxy request
In a project based on vue-cli
, configure the development environment (config/dev.env.js) Setting up a proxy in can redirect all requests starting with /apidomain
to the target interface through the
express
server started by
Official document: https://vuejs-templates.github.io/webpack/proxy.html
proxyTable: { '/apidomain':{ target:'http://localhost:prot',//或ip或域名。 changeOrigin:true, pathRewrite: { '^/apidomain': '' } } },
To access h5 on the LAN through IP, add the host
parameter when starting the development server That is
The dev command configuration of package.json is as follows
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --host 0.0.0.0",
Turn off the chrome security policy to achieve cross-domain
Create a new bat file in windows and paste it The following command can open the CORS cross-domain settings of the asp.net core server in this mode
cd "C:\Program Files (x86)\Google\Chrome\Application" chrome.exe --disable-web-security --user-data-dir=c:/CorsUserData
Official document: https://docs.microsoft.com/ zh-cn/aspnet/core/security/cors
In the actual setting, adding header parameters on the h5 side generates a preflight (OPTIONS) request
. After reading the above article, The general parameters have been modified into the query parameters
1. Add cors service
public void ConfigureServices(IServiceCollection services) { //若只有部分接口则定义一个或多个命名的 CORS 策略,并在运行时按名称然后选择的策略,通过特性标记去设置跨域 详情见文档 services.AddCors(); }
2. Enable middleware
//读取配置文件中设置的允许跨域的域名 CorsOrigins为一个数组 设置["*"]则会允许所有 var origins = Configuration.GetSection("CorsOrigins").GetChildren().Select(s => s.Value).ToArray(); app.UseCors(e => { e.WithOrigins(origins).AllowAnyHeader().AllowAnyMethod().AllowCredentials(); });
//Startup文件中Configuration对象的获取 public IConfiguration Configuration { get; } public Startup() { var builder = new ConfigurationBuilder()//...AddJsonFile($"appsettings.json"); Configuration = builder.Build(); }
above I compiled it for everyone. I hope it will be helpful to everyone in the future.
Related articles:
New features of webpack 4.0.0-beta.0 version (detailed tutorial)
Use SpringMVC to solve vue cross-connection Domain request
The life cycle of Vue components and Route (detailed tutorial)
The above is the detailed content of Vue cross-domain processing issues (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.
