Angular提交的表单在Spring Boot中显示为空
创始人
2024-10-29 18:00:42
0次

在Angular中提交的表单数据在Spring Boot中显示为空的问题可能有多个原因。以下是一些可能的解决方法:

  1. 确保Angular中的表单数据正确地绑定到表单控件上,并在提交时正确地传递给后端。确保表单的ngSubmit事件处理程序正确地处理表单数据,并使用HttpClient将数据发送到后端。例如:

Angular组件:

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-form',
  template: `
    
`, }) export class FormComponent { formData = { name: '' }; constructor(private http: HttpClient) {} submitForm() { this.http.post('/api/submit-form', this.formData).subscribe(response => { console.log(response); }); } }
  1. 在Spring Boot中,确保后端的Controller正确地接收并处理来自Angular的表单数据。使用@RequestBody注解将请求体映射到后端的Java对象中。例如:

Spring Boot Controller:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FormController {

  @PostMapping("/api/submit-form")
  public void submitForm(@RequestBody FormData formData) {
    System.out.println(formData.getName());
    // 处理表单数据
  }
}

public class FormData {
  private String name;

  // 省略getter和setter方法
}
  1. 确保Angular和Spring Boot的跨域配置正确设置。在Spring Boot中,可以使用@CrossOrigin注解来允许来自Angular的跨域请求。在Angular中,可以使用HttpClient的withCredentials选项来处理跨域请求。例如:

Spring Boot Controller:

import org.springframework.web.bind.annotation.CrossOrigin;

@RestController
public class FormController {

  @CrossOrigin(origins = "http://angular-app-url")
  @PostMapping("/api/submit-form")
  public void submitForm(@RequestBody FormData formData) {
    // 处理表单数据
  }
}

Angular组件:

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-form',
  template: `
    
`, }) export class FormComponent { formData = { name: '' }; constructor(private http: HttpClient) {} submitForm() { this.http.post('/api/submit-form', this.formData, { withCredentials: true }).subscribe(response => { console.log(response); }); } }

以上是一些可能的解决方法,但具体解决方法可能因具体情况而异。您可以根据您的代码和需求进行调整和修改。

相关内容

热门资讯

实测发现!五彩茶馆开挂功能神器... 实测发现!五彩茶馆开挂功能神器,五彩茶馆其实存在有开挂神器(有辅助挂方法)1、任何五彩茶馆透视是真的...
一分钟揭秘!天下棋牌辅助器功能... 一分钟揭秘!天下棋牌辅助器功能神器,天下棋牌确实真的是有辅助器神器(有辅助挂秘诀)1、下载好天下棋牌...
热点推荐!星悦城辅助器功能软件... 热点推荐!星悦城辅助器功能软件,星悦城切实是真的有辅助器软件(有辅助挂方式)1、星悦城公共底牌简单,...
分享认知!今日长牌辅助器功能a... 分享认知!今日长牌辅助器功能app,今日长牌竟然真的有辅助器app(有辅助挂透视)一、今日长牌游戏安...
大神推荐!卓越棋牌辅助功能神器... 大神推荐!卓越棋牌辅助功能神器,卓越棋牌总是是真的有辅助神器(有辅助挂总结)1)卓越棋牌免费钻石:进...
我来教大家!茶话汇透视功能教程... 我来教大家!茶话汇透视功能教程,茶话汇其实真的有透视教程(有辅助挂秘籍)1、很好的工具软件,可以解锁...
我来教教你!一健上分辅助器功能... 我来教教你!一健上分辅助器功能方法,一健上分果然存在有辅助器方法(有辅助挂秘籍)1、每一步都需要思考...
新手必备!天际透视功能插件,天... 新手必备!天际透视功能插件,天际原来有透视插件(有辅助挂猫腻)小薇(辅助器软件下载)致您一封信;亲爱...
每日必看!AG真人透视功能软件... 每日必看!AG真人透视功能软件,AG真人一贯有透视软件(有辅助挂教学)1、AG真人免费脚本咨询教程、...
推荐一款!沐星互娱透视挂功能软... 推荐一款!沐星互娱透视挂功能软件,沐星互娱一贯有透视挂软件(有辅助挂神器)1、实时沐星互娱透视辅助更...