gpt4 book ai didi

Angular2 - formData 值作为 null 传递给 HTTP POST

转载 作者:可可西里 更新时间:2023-11-01 17:27:29 26 4
gpt4 key购买 nike

我将我的数据发布到 postman 服务中,它工作正常。但是当我在 HTTP post 请求中调用该服务时,它在控制台中显示成功但 formData 变为 null 并且服务器上没有保存任何内容。

如有任何帮助,我们将不胜感激。我的代码:

import { Component } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Http } from '@angular/http';
import { Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/catch';

@Component({
selector: 'login-page',
templateUrl: './add.html'
})
export class ConfigComponent {
constructor(private http:Http) {
this.news = {
'newstitle': 'Test Title',
'newsdescription': 'Test',
'newstype': 'Test',
'priority': 'Test',
'place': 'Test',
'publishedon': 'Test',
'publishedby': 'Test',
'websiteurl': 'Test',
'contactpersonname': 'Test',
'mobile1': 'Test',
'mobile2': 'Test',
'email': 'Test',
'display': 'Test',
'rating': 'Test'
};
}

onSubmit() {
let formData:FormData = new FormData(this.news);
console.log(this.news);
let headers = new Headers({'encrypt': 'multipart/form-data'});
let options = new RequestOptions({ headers: headers });
this.http.post('SERVICE URL', formData, options)
.subscribe(
data => console.log(data),
error => console.log(error)
);
}
}

Add.html

<h2>ADD CONFIGS</h2>
<div class="row">
<form (ngSubmit)="onSubmit()" #heroForm="ngForm">
<div class="form-group col-xs-6">
<label for="newstitle">NEWS TITLE:</label>
<input type="text" class="form-control" [(ngModel)]="news.newstitle" id="newstitle" name="newstitle"/>
</div>

<div class="form-group col-xs-6">
<label for="newsdescription">NEWS DESCRIPTION:</label>
<input type="text" class="form-control" [(ngModel)]="news.newsdescription" id="newsdescription" name="newsdescription"/>
</div>

<div class="form-group col-xs-6">
<label for="newstype">NEWS TYPE:</label>
<input type="text" class="form-control" [(ngModel)]="news.newstype" id="newstype" name="newstype"/>
</div>

<div class="form-group col-xs-6">
<label for="priority">PRIORITY:</label>
<input type="text" class="form-control" [(ngModel)]="news.priority" id="priority" name="priority"/>
</div>

<div class="form-group col-xs-6">
<label for="place">PLACE:</label>
<input type="text" class="form-control" [(ngModel)]="news.place" id="place" name="place"/>
</div>

<div class="form-group col-xs-6">
<label for="publishedon">PUBLISHED ON:</label>
<input type="text" class="form-control" [(ngModel)]="news.publishedon" id="publishedon" name="publishedon"/>
</div>

<div class="form-group col-xs-6">
<label for="publishedby">PUBLISHED BY:</label>
<input type="text" class="form-control" [(ngModel)]="news.publishedby" id="publishedby" name="publishedby"/>
</div>

<div class="form-group col-xs-6">
<label for="websiteurl">WEBSITE URL:</label>
<input type="text" class="form-control" [(ngModel)]="news.websiteurl" id="websiteurl" name="websiteurl"/>
</div>

<div class="form-group col-xs-6">
<label for="contactpersonname">CONTACT PERSON NAME:</label>
<input type="text" class="form-control" [(ngModel)]="news.contactpersonname" id="contactpersonname" name="contactpersonname"/>
</div>

<div class="form-group col-xs-6">
<label for="mobile1">MOBILE 1:</label>
<input type="text" class="form-control" [(ngModel)]="news.mobile1" id="mobile1" name="mobile1"/>
</div>

<div class="form-group col-xs-6">
<label for="mobile2">MOBILE 2:</label>
<input type="text" class="form-control" [(ngModel)]="news.mobile2" id="mobile2" name="mobile2"/>
</div>

<div class="form-group col-xs-6">
<label for="email">EMAIL:</label>
<input type="email" class="form-control" [(ngModel)]="news.email" id="email" name="email"/>
</div>

<div class="form-group col-xs-6">
<label for="status">STATUS:</label>
<input type="text" class="form-control" [(ngModel)]="news.status" id="status" name="status"/>
</div>

<div class="form-group col-xs-6">
<label for="display">DISPLAY:</label>
<input type="text" class="form-control" [(ngModel)]="news.display" id="display" name="display"/>
</div>

<div class="form-group col-xs-6">
<label for="rating">RATING:</label>
<input type="text" class="form-control" [(ngModel)]="news.rating" id="rating" name="rating"/>
</div>

<div class="form-group col-xs-6">
<label for="usr">City Image:</label>
<img width="50px" height="50px" id="cimage">
<input type="file" (change)="fileChange($event)" placeholder="Upload file">
</div>

<button type="submit" class="btn btn-success">Add</button>

</form>
</div>

enter image description here

最佳答案

将表单值(带文件)附加到 Angular2 中的表单数据。

实际上有人发布了我的问题的答案,实际上归功于他,我在将对象附加到表单数据方面进行了一些修改以使其正常工作。人们可以直接附加表单数据或遍历对象以将值添加到 formData。以下是我的工作代码:

Component.ts

import { Component } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Http } from '@angular/http';
import { Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/catch';

@Component({
selector: 'login-page',
templateUrl: './add.html'
})
export class ConfigComponent {
news;
fileList;
constructor(private http:Http) {
this.news = {
'newstitle': 'Test Title',
'newsdescription': 'Test Title',
'newstype': 'Test Title',
'priority': 'Test Title',
'place': 'Test Title',
'publishedon': 'Test Title',
'publishedby': 'Test Title',
'websiteurl': 'Test Title',
'contactpersonname': 'Test Title',
'mobile1': 'Test Title',
'mobile2': 'Test Title',
'email': 'Test Title',
'status': 'Test Title',
'display': 'Test Title',
'rating': 'Test Title'
};
}

fileChange(event) {
this.fileList = event.target.files;
}

onSubmit() {
let formData:FormData = new FormData();

formData.append('newstitle', this.news.newstitle);
formData.append('newsdescription', this.news.newsdescription);
formData.append('newstype', this.news.newstype);
formData.append('priority', this.news.priority);
formData.append('place', this.news.place);
formData.append('publishedon', this.news.newstitlpublishedone);
formData.append('publishedby', this.news.publishedby);
formData.append('websiteurl', this.news.websiteurl);
formData.append('contactpersonname', this.news.contactpersonname);
formData.append('mobile1', this.news.mobile1);
formData.append('mobile2', this.news.mobile2);
formData.append('email', this.news.email);
formData.append('status', this.news.status);
formData.append('display', this.news.display);
formData.append('rating', this.news.rating);

if(this.fileList.length > 0){
for (var x = 0; x < this.fileList.length; x++) {
let file: File = this.fileList[x];
formData.append('fileData', file, file.name);
}
let headers = new Headers({'encrypt': 'multipart/form-data'});
let options = new RequestOptions({ headers: headers });
this.http.post('SERVICE URL', formData, options)
.subscribe(
data => console.log(data),
error => console.log(error)
);
} else {
let headers = new Headers({'encrypt': 'multipart/form-data'});
let options = new RequestOptions({ headers: headers });
this.http.post('SERVICE URL', formData, options)
.subscribe(
data => console.log(data),
error => console.log(error)
);
}
}
}

ADD.html

<h2>ADD CONFIGS</h2>
<div class="row">
<form (ngSubmit)="onSubmit()" #heroForm="ngForm">
<div class="form-group col-xs-6">
<label for="newstitle">NEWS TITLE:</label>
<input type="text" class="form-control" [(ngModel)]="news.newstitle" id="newstitle" name="newstitle"/>
</div>

<div class="form-group col-xs-6">
<label for="newsdescription">NEWS DESCRIPTION:</label>
<input type="text" class="form-control" [(ngModel)]="news.newsdescription" id="newsdescription" name="newsdescription"/>
</div>

<div class="form-group col-xs-6">
<label for="newstype">NEWS TYPE:</label>
<input type="text" class="form-control" [(ngModel)]="news.newstype" id="newstype" name="newstype"/>
</div>

<div class="form-group col-xs-6">
<label for="priority">PRIORITY:</label>
<input type="text" class="form-control" [(ngModel)]="news.priority" id="priority" name="priority"/>
</div>

<div class="form-group col-xs-6">
<label for="place">PLACE:</label>
<input type="text" class="form-control" [(ngModel)]="news.place" id="place" name="place"/>
</div>

<div class="form-group col-xs-6">
<label for="publishedon">PUBLISHED ON:</label>
<input type="text" class="form-control" [(ngModel)]="news.publishedon" id="publishedon" name="publishedon"/>
</div>

<div class="form-group col-xs-6">
<label for="publishedby">PUBLISHED BY:</label>
<input type="text" class="form-control" [(ngModel)]="news.publishedby" id="publishedby" name="publishedby"/>
</div>

<div class="form-group col-xs-6">
<label for="websiteurl">WEBSITE URL:</label>
<input type="text" class="form-control" [(ngModel)]="news.websiteurl" id="websiteurl" name="websiteurl"/>
</div>

<div class="form-group col-xs-6">
<label for="contactpersonname">CONTACT PERSON NAME:</label>
<input type="text" class="form-control" [(ngModel)]="news.contactpersonname" id="contactpersonname" name="contactpersonname"/>
</div>

<div class="form-group col-xs-6">
<label for="mobile1">MOBILE 1:</label>
<input type="text" class="form-control" [(ngModel)]="news.mobile1" id="mobile1" name="mobile1"/>
</div>

<div class="form-group col-xs-6">
<label for="mobile2">MOBILE 2:</label>
<input type="text" class="form-control" [(ngModel)]="news.mobile2" id="mobile2" name="mobile2"/>
</div>

<div class="form-group col-xs-6">
<label for="email">EMAIL:</label>
<input type="email" class="form-control" [(ngModel)]="news.email" id="email" name="email"/>
</div>

<div class="form-group col-xs-6">
<label for="status">STATUS:</label>
<input type="text" class="form-control" [(ngModel)]="news.status" id="status" name="status"/>
</div>

<div class="form-group col-xs-6">
<label for="display">DISPLAY:</label>
<input type="text" class="form-control" [(ngModel)]="news.display" id="display" name="display"/>
</div>

<div class="form-group col-xs-6">
<label for="rating">RATING:</label>
<input type="text" class="form-control" [(ngModel)]="news.rating" id="rating" name="rating"/>
</div>

<div class="form-group col-xs-6">
<label for="usr">City Image:</label>
<img width="50px" height="50px" id="cimage">
<input type="file" id="newsimg" name="newsimg" multiple="true" (change)="fileChange($event)">
</div>

<button type="submit" class="btn btn-success">Add</button>

</form>
</div>

关于Angular2 - formData 值作为 null 传递给 HTTP POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44327259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com