前后端分离,前端调用后端接口

参考https://blog.csdn.net/Mr_carry/article/details/100537047?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

后端

package com.pp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class TestController {

    // 欢迎页面
    @RequestMapping("/test2")
    @ResponseBody
    public Map test() {

       Map<String,String> map = new HashMap<String, String>();
       map.put("测试1","123");
       map.put("测试2","456");

       return map;
    }
}

允许跨域配置类

package com.pp.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class CorsConfig extends WebMvcConfigurationSupport {
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        System.out.println("----------------------");
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }
}

前端页面

在项目根目录创建一个 vue.config.js文件,可进行baseUrl配置,接口代理以及其他配置

(vue-cli3没有bulid和config文件夹)

module.exports = {
    devServer: {
        proxy: {
            // 配置跨域
            '/api': { //代理接口前缀为/api的请求
                target: 'http://localhost:7007', //需要代理到的目标地址
                ws: true,
                changOrigin: true, //是否跨域
                pathRewrite: {
                    '^/api': '' //重写路径
                }
            }
        }
    }
};

 

https://blog.csdn.net/lihefei_coder/article/details/97630544

https://blog.csdn.net/weixin_38578293/article/details/84823027

<template>
  <div class="info">
    这是从本地后端项目获取的json数据:
    <button v-on:click="postInfo">获取数据</button>
    <br>
    {{this.info}}
  </div>
</template>

<script>
  export default {
    name: 'App',
    data() {
      return {
        info: {}
      }
    },
    methods: {
      postInfo: function () {
        this.$http.post('/api/test2')
                .then(response => (this.info = response))
                .catch(function (error) { // 请求失败处理
                  // eslint-disable-next-line no-console
                  console.log(error);
                });
      }
    }
  }
</script>
<style>
  .info{
    width:100%;
    height: 100%;
  }
</style>

在main.js中:

Vue.prototype.$http = axios

所以是

 this.$http.post('/api/test2')

调用结果:

  • 8
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值