来源已被 CORS 政策 Spring boot 和 React 阻止 [英] origin has been blocked by CORS policy Spring boot and React
本文介绍了来源已被 CORS 政策 Spring boot 和 React 阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个返回所有区对象的控制器
在 'http://localhost:8080/ 处访问 XMLHttpRequest 来自 origin 'http://localhost:3000' 已被 CORS 策略阻止:请求的资源上不存在Access-Control-Allow-Origin"标头.
Access to XMLHttpRequest at 'http://localhost:8080/ from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
package com.ministry.demo.controller;
import com.ministry.demo.model.District;
import com.ministry.demo.repository.DistrictRepository;
import com.ministry.demo.service.DistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(path = "district")
public class DistrictController {
@Autowired
DistrictService service;
@GetMapping(path = "getAll")
List<District> getAllDistrict(){
return service.getAllDistricts();
}
}
推荐答案
我找到了答案
package com.ministry.demo.controller;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping(path = "district")
public class DistrictController {
@Autowired
DistrictService service;
@GetMapping(path = "getAll")
List<District> getAllDistrict(){
return service.getAllDistricts();
}
}
这篇关于来源已被 CORS 政策 Spring boot 和 React 阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文