forms - Symfony 表单与 AngularJS $resource 不兼容

标签 forms angularjs rest symfony post

我有一个由 RESTful Symfony 应用程序支持的 AngularJS 应用程序。我在 POST 用户时遇到问题(例如 POST /api/users/123)。

以下是 Symfony Form 在 POST 请求中期望的内容(编辑或提交资源):

{
  "user": {
    "email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4c2cbcbe4c9cac5d4cbc8cd8ac2d6" rel="noreferrer noopener nofollow">[email protected]</a>"
  }
}

您可以在此处看到正在发布的对象被包装在另一个用户:{…}对象(代表表单)中。

以下是 Angular 的 $resource 在调用 user.$save() 时将发布的内容:

{
  "email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93f5fcfcd3fefdf2e3fcfffabdf5e1" rel="noreferrer noopener nofollow">[email protected]</a>"
}

这里没有包装器。对象直接发布。

  • 哪一个是“正确的”,即 RESTful?
  • 如何让 Symfony 或 Angular 与另一个协同工作(基于之前的答案)?

最佳答案

这是我设法解决它的方法,但我渴望有一个更干净的解决方案(如果存在)。

事实证明,表单类型的名称可以为空:

class UserType extends AbstractType
{
    // ..

    public function getName()
    {
        return '';
    }
}

这使得这是一个有效的提交:

{
  "email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adcbc2c2edc0c3ccddc2c1c483cbdf" rel="noreferrer noopener nofollow">[email protected]</a>"
}

关于forms - Symfony 表单与 AngularJS $resource 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24685826/

相关文章:

html - 验证 |需要 ="true"/"false"| HTML 表格

rest - 冷聚变 10 : Handling and Logging Errors With New REST API

angularjs - Angular 测试: How to expect element events to be triggered?

java - 如何使用 java 8 和 spring jpa 减少空验证码?

wcf - 将 xml 数据发送到 WCF REST 服务

java - 如何在 Freemarker DropWizard 中使用按钮调用方法

python - Django - 从查询字符串获取 ID 到编辑 View 的最佳实践

javascript - 通过表单保存模型后重新渲染 Rails 部分

javascript - 选择 AngularJS 中智能表的所有复选框

angularjs - angularjs 中的 Ucwords 过滤器抛出错误