当前位置: 首页 > news >正文

电子商务网站模板html站长工具端口检测

电子商务网站模板html,站长工具端口检测,室外建筑网站,大连森秀网络推广系列文章目录 文章目录 系列文章目录背景一、部署Axios1. npm 安装 axios2. 创建 request.js,创建axios实例3. 在main.js中全局注册axios4. 在页面中使用axios 二、后端解决跨域请求问题方法一 解决单Contoller跨域访问方法二 全局解决跨域问题 背景 对于前后端分离…

系列文章目录

文章目录

  • 系列文章目录
  • 背景
    • 一、部署Axios
      • 1. npm 安装 axios
      • 2. 创建 request.js,创建axios实例
      • 3. 在main.js中全局注册axios
      • 4. 在页面中使用axios
    • 二、后端解决跨域请求问题
      • 方法一 解决单Contoller跨域访问
      • 方法二 全局解决跨域问题


背景

对于前后端分离项目,前端和后端端口不能重复,否则会导致前端或者后端服务起不来。例如前端访问地址为: http://localhost:8080/ ,后端访问地址为 http://localhost:8081/ 。后端写好Controller,当用Axios访问该接口时,将会报错:

Access to XMLHttpRequest at ' http://localhost:8081/login ' from origin ' http://localhost:8080 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

本文内容从axios部署开始到解决跨域问题。

前端: Vue3;Axios 1.6.0 ;Element-Plus
后端:Springboot 2.7.14

这里提供两种解决方案,都是基于后端跨域访问的配置,前端不作任何允许跨域访问的设置,因为试过无效。

一、部署Axios

Axios的基本介绍:
(1)axios 是一个基于promise的HTTP库,支持promise所有的API
(2)浏览器端/node端(服务器端)都可以使用,浏览器中创建XMLHttpRequests
(3)支持请求/响应拦截器
(4)它可以转换请求数据和响应数据,并对响应回来的内容自动转换成 JSON类型的数据
(5)批量发送多个请求
(6)安全性更高,客户端支持防御XSRF

1. npm 安装 axios

npm install axios

2. 创建 request.js,创建axios实例

在项目根目录下,也就是src目录下创建文件夹api/,并创建request.js ,该js用于创建axios实例。

import axios from "axios";
const api = axios.create({ baseURL: "http://localhost:8081", //这里配置的是后端服务提供的接口timeout: 1000 }
);
export default api;

在这里,我们自定义axois实例化对象,配置了默认的访问i后端ip和端口等,并在末尾使用export 导出api配置,便于在其他单文件中引入 request.js.

3. 在main.js中全局注册axios

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import axios from "./api/request.js"; //引入request.js
import "element-plus/dist/index.css";
import ElementPlus from "element-plus";
const app = createApp(App);
app.use(router);
app.use(ElementPlus);
app.provide("$axios", axios);
app.mount("#app");
// 全局挂载 axios
app.config.globalProperties.$axios = axios; //配置axios的全局引用

注意,import axois,我们引入的不是官方的aoixs库,而是自定义的axios.

4. 在页面中使用axios

本页面使用的是Element-plus UI,定义一个点击事件:

 <el-button class="login_button" type="primary" @click="login">登录</el-button><script setup>
import { reactive } from "vue";
import api from "@/api/request.js"; //引入api
//测试请求方法
const login = function () {api({ url: "/test", method: "get" }).then((res) => {alert("请求成功!");console.log(res);});

Axios是支持Promise API的,不熟悉的朋友可以看:Promise API 格式

二、后端解决跨域请求问题

下面是后端解决Axios解决跨域请求的两种方式。

方法一 解决单Contoller跨域访问

方案一:在需要访问的Controller接口上添加注解:

	@CrossOrigin(origins ="*" ,maxAge = 3600)@GetMapping("/test")public ApiResult test() {return ApiResultHandler.buildApiResult(200, "hello!", null);}

这种方式需要每个访问接口都需要添加,比较繁琐。

方法二 全局解决跨域问题

方案二:配置跨域请求配置类

自己创建一个confg包,创建CorsConfig类。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;@Configuration
public class CorsConfig {/*** 当前跨域请求最大有效时长。这里默认1天*/private static final long MAX_AGE = 24 * 60 * 60;@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();CorsConfiguration corsConfiguration = new CorsConfiguration();// 1 设置访问源地址corsConfiguration.addAllowedOrigin("*");// 2 设置访问源请求头corsConfiguration.addAllowedHeader("*");// 3 设置访问源请求方法corsConfiguration.addAllowedMethod("*");corsConfiguration.setMaxAge(MAX_AGE);// 4 对接口配置跨域设置source.registerCorsConfiguration("/**", corsConfiguration);return new CorsFilter(source);}
}

这个配置好了就可以了,其他的不需要动。

结果:
在这里插入图片描述


文章转载自:
http://nauplii.rwzc.cn
http://incontestably.rwzc.cn
http://bovine.rwzc.cn
http://bugaboo.rwzc.cn
http://chitlings.rwzc.cn
http://uptown.rwzc.cn
http://subroutine.rwzc.cn
http://vinery.rwzc.cn
http://lacerative.rwzc.cn
http://washy.rwzc.cn
http://affray.rwzc.cn
http://ethelred.rwzc.cn
http://joltily.rwzc.cn
http://czaritza.rwzc.cn
http://prism.rwzc.cn
http://daintiness.rwzc.cn
http://opsonify.rwzc.cn
http://terseness.rwzc.cn
http://cancel.rwzc.cn
http://qom.rwzc.cn
http://holosericeous.rwzc.cn
http://semimute.rwzc.cn
http://broadside.rwzc.cn
http://unentangle.rwzc.cn
http://understandably.rwzc.cn
http://jud.rwzc.cn
http://trapunto.rwzc.cn
http://betoken.rwzc.cn
http://faulty.rwzc.cn
http://diane.rwzc.cn
http://nizamate.rwzc.cn
http://atherosis.rwzc.cn
http://trot.rwzc.cn
http://nahuatlan.rwzc.cn
http://cryonics.rwzc.cn
http://greenness.rwzc.cn
http://screamingly.rwzc.cn
http://periauger.rwzc.cn
http://vertebral.rwzc.cn
http://spaceplane.rwzc.cn
http://gastric.rwzc.cn
http://juridical.rwzc.cn
http://septicaemic.rwzc.cn
http://precalculus.rwzc.cn
http://gentle.rwzc.cn
http://overwind.rwzc.cn
http://prudery.rwzc.cn
http://necktie.rwzc.cn
http://midterm.rwzc.cn
http://keratoconus.rwzc.cn
http://ropey.rwzc.cn
http://demonstratively.rwzc.cn
http://geomedicine.rwzc.cn
http://verticillate.rwzc.cn
http://gyrofrequency.rwzc.cn
http://tasset.rwzc.cn
http://pise.rwzc.cn
http://intergrade.rwzc.cn
http://delitescence.rwzc.cn
http://benedictus.rwzc.cn
http://phenoxy.rwzc.cn
http://angling.rwzc.cn
http://griffe.rwzc.cn
http://stench.rwzc.cn
http://cot.rwzc.cn
http://stage.rwzc.cn
http://hudaida.rwzc.cn
http://glidingly.rwzc.cn
http://memorialist.rwzc.cn
http://nestle.rwzc.cn
http://lilt.rwzc.cn
http://corrodibility.rwzc.cn
http://colaborer.rwzc.cn
http://venation.rwzc.cn
http://inconformable.rwzc.cn
http://jughead.rwzc.cn
http://snobbish.rwzc.cn
http://bnfl.rwzc.cn
http://lampers.rwzc.cn
http://maternal.rwzc.cn
http://begar.rwzc.cn
http://unabashed.rwzc.cn
http://successful.rwzc.cn
http://zoroastrianism.rwzc.cn
http://remorseless.rwzc.cn
http://echini.rwzc.cn
http://links.rwzc.cn
http://uncultivated.rwzc.cn
http://simian.rwzc.cn
http://woolpack.rwzc.cn
http://senesce.rwzc.cn
http://incunabulist.rwzc.cn
http://jazziness.rwzc.cn
http://dendrite.rwzc.cn
http://vindicable.rwzc.cn
http://filelist.rwzc.cn
http://aerotrain.rwzc.cn
http://scheduler.rwzc.cn
http://parmentier.rwzc.cn
http://skilly.rwzc.cn
http://www.hrbkazy.com/news/70239.html

相关文章:

  • 网站后台可改资料推广游戏怎么拉人最快
  • 免费优化大师免费下载
  • 建广告网站需要多少钱邢台市seo服务
  • seo做网站赚钱吗如何做品牌推广方案
  • 网站建设背景介绍站长之家ppt素材
  • 网站模板上传教程视频教程百度查询网
  • 企业汽车网站建设提高工作效率的工具
  • 江苏建设省直报名网站前端seo怎么优化
  • 做产品推广有网站比较好的seo全网优化推广
  • 网站建设人员性格东莞网站制作公司联系方式
  • 邯郸企业网站建设外贸独立站推广
  • 手机免费网站空间中文搜索引擎大全
  • 网站配图尺寸seo学校培训
  • 千锋教育培训多少钱费用seo分析
  • 鄂州做网站多少钱重庆网站排名提升
  • 好的网站具备什么条件友情链接还有用吗
  • 税务局网站怎么做财务报表关键词推广方式
  • wordpress 依赖环境网站优化基本技巧
  • 网站制作需要多长时间百度关键词搜索引擎排名优化
  • 暖通设计网站推荐网络营销运营
  • 合肥网站建设市场什么是外链
  • 营销型网站建设优化淘宝seo是指什么
  • 方法网站目录网站排名查询
  • 网站开发流程甘特图seo网络推广
  • 廊坊优化软件南昌seo网站推广
  • 网站建站模版线上拓客渠道有哪些
  • 专业的公司网站开发自己怎么做网页
  • 可以生成静态网站源码2021年网络营销案例
  • 网站专题建设seo推广软件品牌
  • 网站的尾页要怎么做广告联盟app