问题 Cors:请求的资源上不存在 'Access-Control-Allow-Origin' header
Problem Cors: No 'Access-Control-Allow-Origin' header is present on the requested resource
我无法解决这个错误;我加了
@CrossOrigin(origins="http://localhost:4200")
但错误仍然存在。
RestAPIController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api/customer")
public class RestAPIController {
@Autowired
CustomerServices customerServices;
@PostMapping("/create")
public ResponseEntity<Message> addNewCustomer(@RequestBody Customer customer) {
try {
Customer returnedCustomer = customerServices.saveCustomer(customer);
return new ResponseEntity<Message>(new Message("Upload Successfully!",
Arrays.asList(returnedCustomer), ""), HttpStatus.OK);
}catch(Exception e) {
return new ResponseEntity<Message>(new Message("Fail to post a new Customer!",
null, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
.
.
.
.
}
你建议我做什么?谢谢
各种方法都试过了,我在这个网站上找到并尝试了几个例子,但问题仍然存在。
这是一个老问题。
您应该在扩展 WebMvcConfigurer
的配置 class 中添加此覆盖方法
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("http://localhost:3000")
.maxAge(3600);
}
我无法解决这个错误;我加了 @CrossOrigin(origins="http://localhost:4200") 但错误仍然存在。
RestAPIController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
(origins = "http://localhost:4200")
@RestController
@RequestMapping("/api/customer")
public class RestAPIController {
@Autowired
CustomerServices customerServices;
@PostMapping("/create")
public ResponseEntity<Message> addNewCustomer(@RequestBody Customer customer) {
try {
Customer returnedCustomer = customerServices.saveCustomer(customer);
return new ResponseEntity<Message>(new Message("Upload Successfully!",
Arrays.asList(returnedCustomer), ""), HttpStatus.OK);
}catch(Exception e) {
return new ResponseEntity<Message>(new Message("Fail to post a new Customer!",
null, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
.
.
.
.
}
你建议我做什么?谢谢
各种方法都试过了,我在这个网站上找到并尝试了几个例子,但问题仍然存在。
这是一个老问题。
您应该在扩展 WebMvcConfigurer
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("http://localhost:3000")
.maxAge(3600);
}