【问题标题】:403 error for CORS preflight OPTIONS request. How to fix?CORS 预检选项请求的 403 错误。怎么修?
【发布时间】:2019-10-10 20:25:34
【问题描述】:

我的项目是创建一个输入页面,用于在里面输入一些文本并将其发送到 mysql (phpmyadmin) 。我正在使用 spring-boot 2.1.4 和 angular 7。 提前感谢您的调查!爱

我专注于 GraphController.java 并尝试使用 @CrossOrigin 进行多种选择。我试图在全球范围内调用它,但没有... 这是我的来源https://spring.io/blog/2015/06/08/cors-support-in-spring-framework 我也什么都没试过

我的实体(Graph.java)

@Entity(name = "graphiques")
@Table(name ="graphiques")
@Data
@NoArgsConstructor
@AllArgsConstructor

public class Graph {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name ="id")
  private Long id;
  @Column(name ="xml")
  private String xml;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getXml() {
    return xml;
  }

  public void setXml(String xml) {
    this.xml = xml;
  }

}

我的 GraphController.java

@RestController
@CrossOrigin
@RequestMapping("/insert")
public class GraphController {
  @Autowired
  GraphRepository graphRepository;

  @GetMapping
  @ResponseBody
  public String addGraph(@RequestParam String xml) {
    Graph graph = new Graph();
    graph.setXml(xml);
    graphRepository.save(graph);
    String ret = "Graph has been added";
    return ret;
  }

我在 Angular 中的 xml-insert-form.component.ts

  insertForm: FormGroup;
  xml: string;
  submitted = false;
  graph: Graph;

  initForm() {
    this.insertForm = this.formBuilder.group({
      xml: ['', Validators.required]
    });
  }
  onSubmit() {
    const xml = this.insertForm.get('xml').value;
    const newGraph = new Graph(xml);
    this.graphService.createNewGraph(newGraph).subscribe(
      graph => {
        console.log('onSubmit OK');
      },
      error => console.log('Erreur lors de l\'ajout du nouveau graph')
    );
    this.submitted = true;
  }

在 mysql 中,我得到了 1 个名为“sogetiauditback”的数据库和一个名为“graphiques”的表,其中包含“id”和“xml”列。 (xml 将是输入文本中的纯文本)

预期结果:没有 403 错误,输入中的我的数据发送到 mysql 错误消息(谷歌浏览器:
- polyfills.js:3251 OPTIONS http://localhost:8282/insert 403 - Access to XMLHttpRequest at 'http://localhost:8282/insert' from origin 'http://localhost:4200' 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.)

【问题讨论】:

标签: java angular spring-boot cors


【解决方案1】:

添加如下 CORS 配置:

CORSConfig.java

@Configuration
public class CORSConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD");
    }
}

【讨论】:

  • 好像没问题!非常感谢 !现在我遇到了这个问题 Request method 'POST' not supported 不再使用 Cors 了。
【解决方案2】:

您可以使用“Access-Control-Allow-Origin”标头在客户端配置 cors 问题

或者尝试使用这个

@CrossOrigin(origins = "http://localhost:4200")
@GetMapping
public String addGraph(@RequestParam String xml) {
   Graph graph = new Graph();
   graph.setXml(xml);
   graphRepository.save(graph);
   String ret = "Graph has been added";
   return ret;
}

【讨论】:

  • 我不知道在哪里添加“Access-Control-Allow-Origin”标头(Google 不会帮助我)而且我已经尝试过您的代码。但同样的错误,感谢您的快速回复:)
  • 使用 @CrossOrigin(origins = "*") 因为这对我有帮助。如果这对我显示你的 Angular api 请求没有帮助
  • 我也已经这样做了,但是 Debendra 解决了我的问题!感谢尝试帮助我 lm1010 :)
猜你喜欢
  • 2019-10-10
  • 2020-04-09
  • 2013-09-30
  • 2015-08-10
  • 2020-10-26
  • 2017-05-06
  • 2019-10-13
  • 2019-04-11
  • 2018-08-22
相关资源
最近更新 更多