React+ SpringBoot 跨域问题

react

1.新建CORSConfiguration.java 文件

2.编写代码

package com.vuvivian.jianshu;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.CorsRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration

public class CORSConfiguration extends WebMvcConfigurerAdapter {

@Override

public void addCorsMappings(CorsRegistry registry){

registry

.addMapping("/**")

.allowedMethods("*")

.allowedOrigins("*")

.allowedHeaders("*");

}

}

3.写个接口,新建类:HelloController ,编写代码

package com.vuvivian.jianshu;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class HelloController {

@RequestMapping(value = "/hello",method = RequestMethod.GET)

public String say() {

return "区块链";

}

}

4.React调用axios方法,调接口

  axios.get('http://localhost:8080/hello').then((res)=>{

const data = res.data;

console.log(data)

}).catch(()=>{

console.log("error")

})

5.打开页面,查看结果

以上是 React+ SpringBoot 跨域问题 的全部内容, 来源链接: utcz.com/z/384178.html

回到顶部