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); }); } }

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

相关内容

热门资讯

WPK透视版透视项目!微扑克透... WPK透视版透视项目!微扑克透视脚本,德扑之星ai多功能透视方法微扑克透视脚本辅助器是一种具有地方特...
热点推荐透视挂!闲娱麻将ai透... 热点推荐透视挂!闲娱麻将ai透视挂多功能插件,果然是真的有透视挂脚本妙招是一款可以让一直输的玩家,快...
WPK插件透视积累!wepok... WPK插件透视积累!wepoker亲友圈有用吗,微扑克ai多功能透视神器1.wepoker亲友圈有用...
新手必备辅助!大宝苏北麻将ai... 您好,大宝苏北麻将这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款...
WPK购买辅助器练习!wepo... WPK购买辅助器练习!wepoker代打辅助,WePoKerai多功能辅助器技巧1、下载好wepok...
让我来分享经验辅助挂!山西大唐... 您好:山西大唐这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,...
WPK下载辅助器讲义!wepo... WPK下载辅助器讲义!wepoker如何设置透视,AApokerai多功能辅助器app1、完成wep...
六分钟了解辅助!决胜麻将ai辅... 六分钟了解辅助!决胜麻将ai辅助多功能插件,素来真的是有辅助助手教材AI 智能体黑科技再上新,自动化...
WPK有透视辅助器模板!德普之... WPK有透视辅助器模板!德普之星透视辅助插件,AApokerai多功能辅助器脚本该软件可以轻松地帮助...
教程辅助开挂!雅苑棋牌ai开挂... 很多人使用云端 AI 时,都担心雅苑棋牌、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技,很...