我正在研究Angular 2技术,在我当前的一个项目中,我遇到了使用打字稿代码的HTTTP POST请求中的问题。
这是我在 myservice.ts
中编写的代码 postVehicleInfoRestful(vehicleInfo: VehicleInfoTable): Observable<VehicleInfoTable> {
console.log('VehicleInfo Service.')
//let body = JSON.stringify(vehicleInfo ? vehicleInfo : null);
let body = JSON.stringify(vehicleInfo);
console.log(body);
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
}); //'application/x-www-form-urlencoded' application/json
let options = new RequestOptions({ headers: headers });//, method: "post"
console.log(this._settingsService.todoListAPIUrl + '/api/VehicleInfoTable');
return this._http.post(this._settingsService.todoListAPIUrl + '/api/VehicleInfoTable', body, options)
.map(res => res.json())
.do(data => console.log(JSON.stringify(data)))
.catch(this.handleError);
}
此外,在 mycomponenet.ts
中编写了代码ngOnInit(): void {
console.log(' VehicleInfoComponent In OnInit');
this.vehicleInfo.ID = 7;
this.vehicleInfo.AssetType = 'Auto';
this.vehicleInfo.IntendedUse = 'Personal';
this.vehicleInfo.NeworUsed = 'New';
this.vehicleInfo.SSN = '123456789';
this.vehicleInfo.VehicleYear = '1';
this.vehicleInfo.VehicleMake = '2';
this.vehicleInfo.VehicleModel = '3';
this.vehicleInfo.VIN = 'US123';
this.vehicleInfo.CurrentMileage = '40';
}
saveVehicleInfo() {
console.log('Save Vehicle Info Details.');
console.log(this.vehicleInfo);
this._vehicleInfoService.postVehicleInfoRestful(this.vehicleInfo).subscribe(//call the post
data => this.postVehicleInfoToServer = JSON.stringify(data),// put the data returned from the server in our variable
error => console.log("Error HTTP Post Service"),// in case of failure show this message
() => console.log("Job Done Post !")); //run this code in all cases
}
这是我在 mycontroller.cs
中编写的代码 public async Task Post(VehicleInfoTable vehicleInfo)
{
try
{
//owner = ((ClaimsIdentity)User.Identity).FindFirst(ClaimTypes.NameIdentifier).Value;
using (var client = NewDataAPIClient())
{
await client.VehicleInfoTables.PostVehicleInfoTableByVehicleinfotableAsync(new VehicleInfoTable
{
ID = (int)vehicleInfo.ID,
AssetType = vehicleInfo.AssetType,
IntendedUse = vehicleInfo.IntendedUse,
NeworUsed = vehicleInfo.NeworUsed,
SSN = vehicleInfo.SSN,
VehicleYear = vehicleInfo.VehicleYear,
VehicleMake = vehicleInfo.VehicleMake,
VehicleModel = vehicleInfo.VehicleModel,
VIN = vehicleInfo.VIN,
CurrentMileage = vehicleInfo.CurrentMileage
});
}
System.Diagnostics.Trace.TraceInformation("VehicleInfoTableController in ToDoListAPI");
DevInsights.TrackEvent("VehicleInfoTableController in ToDoListAPI", "Post");
}
catch (Exception ex)
{
DevInsights.TrackException(ex, "VehicleInfoTableController in ToDoListAPI", "Post");
System.Diagnostics.Trace.TraceWarning(ex.Message);
}
}
当我调试我的代码时,一旦点击保存按钮,根据我的帖子url这个方法会触发公共异步任务帖子(VehicleInfoTable vehicleInfo) { ...... } 但是, vehicleInfo 参数包含所有字段值 null 但是当我发布请求时,我在正文中添加了数据。
你能否告诉我在http post请求中将数据添加到body的错误。
你能否告诉我我错在哪里?如何解决这个问题?
-Pradeep
答案 0 :(得分:2)
编辑我可以建议您尝试两种选择:
试试这个,首先让它尽可能简单,然后尝试添加更多移动部件;)
postVehicleInfoRestful(vehicleInfo: VehicleInfoTable) {
let body = JSON.stringify(vehicleInfo);
let headers = new Headers({
'Content-Type': 'application/json'});
let options = new RequestOptions({ headers: headers });
return this._http.post(this._settingsService.todoListAPIUrl + '/api/VehicleInfoTable', body, options)
.map((res: Response) => { console.log('res', res); }) // let's see what we get
}
或:
postVehicleInfoRestful(vehicleInfo: VehicleInfoTable) {
let body = new URLSearchParams();
// mark all your properties and values below with body.set
body.set('yourPropertyName1', vehicleInfo.property1)
body.set('yourPropertyName2', vehicleInfo.property2)
let headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded'});
let options = new RequestOptions({ headers: headers });
return this._http.post(this._settingsService.todoListAPIUrl + '/api/VehicleInfoTable', body.toString(), options)
.map((res: Response) => { console.log('res', res); }) // let's see what we get
}
答案 1 :(得分:0)
在map(res => console.log(res.json()))
中的Observable上调用myservice.ts
会将结果映射到类型void
。改为纠正map(res => res.json())
。 RXJS Observable do(...)
方法完全是为此目的而设计的,因为它不会影响Observable的内容。