axios 跨域问题_前端跨域产生的原因和解决方法

axios 跨域问题_前端跨域产生的原因和解决方法首先,经典报错:No‘Access-Control-Allow-Origin’解决方法:一、配置main.js此处已经默认请求都添加/api为前缀importVuefrom’vue’importAppfrom’./App.vue’importrouterfrom’./router’importaxiosfrom’axios’import’font-awesome/css/font-awesome.min.css’Vue.config.product

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

首先,经典报错:No ‘Access-Control-Allow-Origin’

解决方法:

一、配置main.js

此处已经默认请求都添加/api为前缀

import Vue from 'vue'
import App from './App.vue'
import router from './router'

import axios from 'axios'
import 'font-awesome/css/font-awesome.min.css'

Vue.config.productionTip = false

axios.defaults.baseURL='/api/'
Vue.prototype.$axios = axios


new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

 二、配置config.index.js

也就是在proxyTable中写上目标地址,主要是已经重写过/api了,之后的axios请求中都不需要再添加/api,也就是

pathRewrite: {

          “^/api”:”

        }

不需要写作下面这样, 会重复导致报错。

pathRewrite: {

          “^/api”:“/api”

        }

正确的index.js代码:

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation.

const path = require('path')

module.exports = {
  dev: {

    // Paths
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: { // 配置代理,下面是个例子
      '/api': {
        target: 'http://localhost:8880',
        changeOrigin: true,
        ws:true,
        pathRewrite: {
          "^/api":''
        }
      }
    },
 
    // host: 'localhost', // can be overwritten by process.env.HOST
    host: '0.0.0.0', // can be overwritten by process.env.HOST
    port: 3000, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: false,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false,  
    useEslint: true,
 
    showEslintErrorsInOverlay: false,
    devtool: 'cheap-module-eval-source-map',
    cacheBusting: true,
    cssSourceMap: true
  },

  build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
   // assetsPublicPath: '/',
    assetsPublicPath: './', // 生产环境打包后静态文件路径修改为相对路径
    /**
     * Source Maps
     */

   // productionSourceMap: true,
    productionSourceMap: process.env.env_config !== 'prod', // 生产环境不需要sourceMap,测试阶段可为true
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],
    bundleAnalyzerReport: process.env.npm_config_report || true
  }
}

三、写请求:get请求为例

axios.get('/student',{//你想访问的资源
  params:{
    name:"邹xx"//因为后端使用findbyname函数
  }
})
.then(function(response){
  console.log(response);
})
.catch(function(error){
  console.log(error);
});

四、顺手又写出404

404 Not Found
请求失败,请求所希望得到的资源未被在服务器上发现

axios 跨域问题_前端跨域产生的原因和解决方法

 没有这个路径,

@RestController

public class Studentcontroller {

    @Autowired
    StudentService studentService;

    @RequestMapping("/deletestudent")
    public void deletebyID(){
//        Optional<StudentEntity> byId = studentRepository.findById(13869L);
//        byId.orElse(null);
//        StudentEntity studentEntity = new StudentEntity();
//        studentEntity.setName("xxb");
//        studentRepository.save(studentEntity);
    }

    @GetMapping("/{name}")
    public StudentEntity findByName(@PathVariable("name") String name){
        return studentService.findByName(name);
    }

查看自己的路径,就是服务器端的问题

GetMapping 注解已经默认封装了@RequestMapping

使用postman测试

数据库中此人确实存在:

axios 跨域问题_前端跨域产生的原因和解决方法

参数理解:

@GetMapping(value = "/service", params = "serviceName=CREATE_PROJECT")

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194900.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【组合数求模】 转自AekdyCoin

    【组合数求模】 转自AekdyCoin大家都在中学阶段学习了组合数的定义:这个表示的是从n个元素中选取m个元素的方案数。(PS.组合数求模似乎只用在信息学竞赛和ACM竞赛等计算机编程设计大赛中……,求在现实中的运用) 可以知道当n,m 取得比较大的时候,组合数可能很大很大(天文数字?无法度量?)例如C(100,50)=100891344545564193334812497256, 于是计算机的64

    2022年7月23日
    10
  • php源码审计_静态代码审计

    php源码审计_静态代码审计最近在学PHP代码审计,那就将学习的笔记都整理一遍吧~前期准备:1、安装相关软件,如Sublimetext、 Notepad++、editplus、 Seay源代码审计系统等2、获得源码,可以到网上下载各种网站源码3、安装网站审计方法:通读全文法:麻烦但全面敏感函数参数回溯法:高效常用,Seay源代码审计系统定向功能分析法:主要根据程序的业

    2022年10月2日
    5
  • java integer long 转换_long(Long)与int(Integer)之间的转换

    java integer long 转换_long(Long)与int(Integer)之间的转换1.将long型转化为int型,这里的long型是基础类型:longa=10;intb=(int)a;2.将Long型转换为int型的,这里的Long型是包装类型:Longa=10;intb=a.intValue();3.将Long型转换为Integer型的,这里的Long型是包装类型:Longa=10;;Integerb=a.intValue(…

    2022年6月5日
    42
  • 渗透测试常用工具汇总_渗透测试实战

    渗透测试常用工具汇总_渗透测试实战目录1.Wireshark2.Metasploit3.Nmap4.Nessus5.SQLMap6.W3af1.WiresharkWireshark(前称Ethereal)是一个网络分包分析软件,是世界上使用最多的网络协议分析器。Wireshark兼容所有主要的操作系统,如Windows、Linux、macOS和Solaris。kali系统里面自带有这个软件,我们可以直接使用;或者可以在网上下载windows版本,在windows系统里使用。..

    2022年8月12日
    7
  • Centos下安装yum(完整教程)

    Centos下安装yum(完整教程)在安装yum的时候发现网上的方法参差不齐,不是很完整,以下是个人亲测,同时是总结出的最方便的方法,希望能帮助到大家!查看已安装的yumrpm-qa|grepyum删除已有的yumrpm-aq|grepyum|xargsrpm-e–nodeps下载以下安装包wgethttp://tel.mirrors.163.com/centos/7/os/x86_64/Packages/python-2.7.5-89.el7.x86_64.rpmwgethttp:.

    2022年6月4日
    178
  • STM32 SPI详解[通俗易懂]

    STM32 SPI详解[通俗易懂]本文的程序是主控室STM32F107各种宏定义和文件会在末尾说明1、SPI简介SPI,是英语SerialPeripheralinterface的缩写,顾名思义就是串行外围设备接口。是Motorola首先在其MC68HCXX系列处理器上定义的。SPI接口主要应用在EEPROM,FLASH,实时时钟,AD转换器,还有数字信号处理器和数字信号解码器之间。SPI,是一种高速的,全双工,

    2022年6月18日
    38

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号