我發佈我的數據到postman服務,它工作正常。但是當我在HTTP post請求中調用服務時,它在控制檯中顯示成功,但formData變爲空,並且沒有任何內容保存在服務器上。Angular2 - formData值作爲NULL傳遞給HTTP POST
任何幫助,將不勝感激。我的代碼:
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>
['FormData'構造(HTTPS:
然後,
post
可以這樣做: //developer.mozilla.org/en-US/docs/Web/API/FormData/FormData)需要一個表單元素。你爲什麼傳遞一個對象? – Saravana