控制台错误
Access to XMLHttpRequest at 'http://localhost:9191/api/v1/employee' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Cors已经在全局后端配置,并且它可以工作。
这里有两个端点,非常标准,除了Post不起作用,Get可以:
@PostMapping(value = "/employee")
@ResponseBody
public EmployeeDTO createEmployee(EmployeeDTO employee){
return employeeService.createEmployee(employee);
}
@GetMapping("/employee")
public List<EmployeeDTO> getEmployees(){
return employeeService.getAllEmployees();
}
您需要配置您的后端以允许crodd=origin请求。
你可以在Spring Boot控制器中使用@CrossOrigin注释来指定是否允许请求的起源、方法和头。
只需将控制器类注释为@CrossOrigin,如下所示。
@RestController
@CrossOrigin(origins = "http://localhost:9191", methods = {
RequestMethod.POST,
RequestMethod.DELETE,
RequestMethod.PUT})
public class YourController {
}
在你的react应用程序中,你可能需要在你的HTTP请求中设置'Access-Control-Allow-Origin'头。
Axios.post('http://localhost:9191/yourUrl',{
// your request body
})