我正在尝试使用以下代码发送formData,但是当我控制台formData时,我得到的是空的formData
我是初学者,我真的很困惑为什么会发生这种情况,请告诉我为什么我遇到问题可以帮助我一个人
我在这里记录formdata FormData {}
onSubmit () {
const formData = new FormData();
formData.append('title', this.blogForm.get('title').value);
formData.append('description', this.blogForm.get('description').value);
formData.append('is_featured', this.blogForm.get('is_featured').value);
formData.append('is_active', this.blogForm.get('is_active').value);
formData.append('image', this.blogForm.get('image').value);
console.log(formData);
this.blogService.createBlog(formData).subscribe(
res => {
if (!res) {
this.uploadError = "Error occured";
} else {
this.router.navigate(['/admin/blogs']);
}
},
error => this.error = error
);
createBlog(blog) {
return this.http.post<Blog>(this.serverUrl + 'admin-blog-list', blog)
.pipe(
catchError(this.handleError)
);
}
答案 0 :(得分:1)
由于 formData 是一个对象,因此要访问其值,必须使用
console.log(formData.get('title'))
for (var key of formData.keys()) {
console.log(key);
}
for (var value of formData.values()) {
console.log(value);
}
请参阅https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData
答案 1 :(得分:0)
嗨,嗨,我不确定您是否会获得this.blogForm.get(*)。value,但是如果获得您的价值,请立即尝试解决问题
onSubmit () {
//Change this of code
let formdata: FormData = new FormData();
formData.append('title', this.blogForm.get('title').value);
formData.append('description', this.blogForm.get('description').value);
formData.append('is_featured', this.blogForm.get('is_featured').value);
formData.append('is_active', this.blogForm.get('is_active').value);
formData.append('image', this.blogForm.get('image').value);
console.log(formData);
this.blogService.createBlog(formData).subscribe(
res => {
if (!res) {
this.uploadError = "Error occured";
} else {
this.router.navigate(['/admin/blogs']);
}
},
error => this.error = error
);
createBlog(blog) {
return this.http.post<Blog>(this.serverUrl + 'admin-blog-list', blog)
.pipe(
catchError(this.handleError)
);
}
如果仍然无法正常工作,请告诉我