带有 React 的 Spring Boot
CORS 策略已阻止从源“ http://localhost:3000 ”访问“ http://localhost:8080/ ”处的 XMLHttpRequest :
这是一个返回所有区对象的控制器
CORS 策略已阻止在 ' http://localhost:8080/从源 ' http://localhost:3000 '访问 XMLHttpRequest :请求的资源上不存在 'Access-Control-Allow-Origin' 标头。
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();
}
}