You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular5+Symfony3.4(FOSUserBundle)如何构造嵌套JSON的POST请求?

调整Angular请求JSON结构适配Symfony FOSUserBundle API要求

嘿,这事儿解决起来超简单!你只需要调整请求体的字段嵌套结构,把密码相关的参数放进plainPassword对象里就行,修改后的Angular注册方法代码如下:

register(username: string, email: string, first: string, second: string) {
  // 重组请求参数,将密码字段嵌套到plainPassword对象中
  const requestBody = {
    username,
    email,
    plainPassword: {
      first,
      second
    }
  };

  return this.appHttp.post('register', requestBody).subscribe(
    data => {
      this.appHttp.logRequest(data);
    }
  );
}

修改后发送的JSON结构就完全匹配API要求了:

{ "email": "email@test.com", "username" : "userTest", "plainPassword" : { "first" : "123", "second": "123" } }

核心就是把原本平级的first和second字段,放到plainPassword这个嵌套对象里,完美契合FOSUserBundle的注册接口参数规则。

内容的提问来源于stack exchange,提问作者Lucas Dmnt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:20:26