我想弄清楚我做错了什么。我收到400个错误的请求,试图通过角度服务发送发布请求。我有2个实体 - Document和DocumentCategory(多对多关系)。我可以发布文档本身(没有类别),没有问题。400错误请求Symfony 3与角度
文档create.component.ts
createDocument(title, body, categories) {
let document = {title: title, body: body, categories: categories};
this._crudService.createDocument(document).subscribe(
data => {
return true;
},
error => {
console.error("Error saving document! " + error);
return Observable.throw(error);
}
);
}
crudService.ts
createDocument(document) {
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
//let body = JSON.stringify(document);
let body = document;
return this.http.post
('http://localhost:8000/documents', body, headers);
}
形式
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('title', TextType::class)
->add('body', TextType::class)
//->add('categories', TextType::class)
->add('categories', EntityType::class, array(
'class' => 'AppBundle:DocumentCategory',
'multiple' => true,
'expanded' => true,
'by_reference' => false,
'choice_label' => 'id',
))
;
}
Document.php
/**
* @ORM\ManyToMany(targetEntity="DocumentCategory", mappedBy="documents")
* @JMSSerializer\Expose
*/
private $categories;
DocumentCategory.php
/**
* @ORM\ManyToMany(targetEntity="Document", inversedBy="categories")
* @ORM\JoinTable(name="document_category_document")
* @JMSSerializer\Expose
*/
private $documents;
请求
POST /文件HTTP/1.1 接受:应用/ JSON,文本/纯,/ Accept-Encoding:gzip,deflate,br 接受语言:EN-US,EN; Q = 0.8 连接:保持活跃 的Content-Length:213 内容类型:应用程序/ JSON 主机:本地主机:8000 产地:http://localhost:4200 的Referer:http://localhost:4200/admin/document/create 用户-Agent:Mozilla/5.0(X11; Linux的x86_64的)为AppleWebKit/537.36(KHTML,例如Gecko)Chrome浏览器/ Safari浏览器55.0.2883.87/537.36 X-PHP-OB-等级:1
{
"title": "t",
"body": "<p>b</p>",
"categories": [
{
"id": 1,
"name": "cat1",
"documents": []
},
{
"id": 2,
"name": "cat2",
"documents": []
}
]
}
正如我所说的,如果我删除类别,一切正常。我无法弄清楚:(
编辑:邮差这显示在回应时,我尝试发送上述JSON作为应用/ JSON:
{
"children": {
"title": {},
"body": {},
"categories": {
"errors": [
"This value is not valid."
],
"children": {
"1": {},
"2": {},
"3": {},
"4": {}
}
}
}
}
请使用类似邮递员的方式将数据发送到后端。这将指定它是否和角度问题或后端。我假设它的后端相关。 – hogan
请参阅编辑 - 为什么类别无效? – Tompo
它必须是后端--JSON是有效的,我可以发布文档和类别,但不参考。 – Tompo