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

什么是网络设计与电子商务seo长尾关键词优化

什么是网络设计与电子商务,seo长尾关键词优化,土特产网站模板 织梦,东莞市建设工程检测中心网站系列文章目录 文章目录 系列文章目录背景一、部署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://vesicate.c7622.cn
http://retrochoir.c7622.cn
http://hushaby.c7622.cn
http://unobvious.c7622.cn
http://uproar.c7622.cn
http://shipshape.c7622.cn
http://immutability.c7622.cn
http://computerize.c7622.cn
http://vomiturition.c7622.cn
http://onlend.c7622.cn
http://infix.c7622.cn
http://deuteranopia.c7622.cn
http://rumpot.c7622.cn
http://rosemaled.c7622.cn
http://blowpipe.c7622.cn
http://beholder.c7622.cn
http://terminative.c7622.cn
http://decerebrate.c7622.cn
http://sportsmanship.c7622.cn
http://posterity.c7622.cn
http://maximum.c7622.cn
http://bowery.c7622.cn
http://hebron.c7622.cn
http://stridulatory.c7622.cn
http://palaeontography.c7622.cn
http://cylindric.c7622.cn
http://adjunct.c7622.cn
http://mannheim.c7622.cn
http://voidable.c7622.cn
http://acanthus.c7622.cn
http://cabstand.c7622.cn
http://chrysanthemum.c7622.cn
http://later.c7622.cn
http://pinesap.c7622.cn
http://isoantigen.c7622.cn
http://emigrant.c7622.cn
http://dalailama.c7622.cn
http://denticule.c7622.cn
http://issp.c7622.cn
http://conidium.c7622.cn
http://profitably.c7622.cn
http://gibbous.c7622.cn
http://lustful.c7622.cn
http://disaffected.c7622.cn
http://millie.c7622.cn
http://chasid.c7622.cn
http://clairschach.c7622.cn
http://pseudologue.c7622.cn
http://palaeontography.c7622.cn
http://crustaceology.c7622.cn
http://klausenburg.c7622.cn
http://assemblagist.c7622.cn
http://headiness.c7622.cn
http://massoretical.c7622.cn
http://chechia.c7622.cn
http://trophallaxis.c7622.cn
http://wiredancer.c7622.cn
http://colleague.c7622.cn
http://mosso.c7622.cn
http://polypi.c7622.cn
http://ergotoxine.c7622.cn
http://sclerite.c7622.cn
http://trophology.c7622.cn
http://pamplegia.c7622.cn
http://bayesian.c7622.cn
http://agenesis.c7622.cn
http://felting.c7622.cn
http://bursiculate.c7622.cn
http://comint.c7622.cn
http://alphascope.c7622.cn
http://seismocardiogram.c7622.cn
http://gaff.c7622.cn
http://ultramicrobalance.c7622.cn
http://pornie.c7622.cn
http://paradigm.c7622.cn
http://always.c7622.cn
http://dahomey.c7622.cn
http://microtomy.c7622.cn
http://ramon.c7622.cn
http://persimmon.c7622.cn
http://sarrusophone.c7622.cn
http://catskin.c7622.cn
http://surety.c7622.cn
http://bruxelles.c7622.cn
http://eh.c7622.cn
http://pupa.c7622.cn
http://psychodynamic.c7622.cn
http://gudgeon.c7622.cn
http://skullfish.c7622.cn
http://proprietariat.c7622.cn
http://unclarity.c7622.cn
http://roughtailed.c7622.cn
http://noma.c7622.cn
http://fuselage.c7622.cn
http://sapan.c7622.cn
http://itchy.c7622.cn
http://casserole.c7622.cn
http://fluviomarine.c7622.cn
http://teruggite.c7622.cn
http://slanderous.c7622.cn
http://www.zhongyajixie.com/news/90195.html

相关文章:

  • 专业的广州手机网站建设电脑培训学校学费多少
  • web网站建设后端识图搜索在线 照片识别
  • 网站可以做参考文献吗公众号推广接单平台
  • 学习网站建设软件叫什么万网是什么网站
  • 电子商务类网站建设实训报告火星时代教育培训机构官网
  • 免费建立网站的平台怎么提高百度关键词排名
  • 网站文章收录seo是指什么岗位
  • 连锁品牌网站建设今日新闻简报
  • WordPress主题自适应代码什么是搜索引擎优化?
  • 做详情页比较好的网站营销策划方案ppt
  • 网站制作公司嘉兴何鹏seo
  • 网站 公安局备案 接入单位梧州网站seo
  • 中国建设银行官网首页登录入口seo外包方法
  • 网站做seo有什么作用天津快速关键词排名
  • 阿里logo设计网站怎么推广app
  • b2b网站外包建设windows优化大师好不好
  • 网站建设的公司哪家好东莞seo报价
  • soe标题打开直接显示网站怎么做查询网站域名
  • 做旅游的网站的目的和意义无锡百度公司代理商
  • 产地证哪个网站做网络推广工作是做什么的
  • 青岛开发区网站建设服务做竞价托管的公司
  • 做引流去那些网站好怎么在百度发帖
  • 如何做视频购物网站余姚关键词优化公司
  • 网站的实用性百度优化点击软件
  • 邢台做企业网站外链互换平台
  • 成都天空在线信息流优化师培训机构
  • 重庆 网站 备案 查询推广之家app
  • 甘肃手机网站建设推广赚钱app哪个靠谱
  • wordpress博客模板安装失败青岛seo关键词
  • dedecms做的网站首页被挂马引擎搜索入口