【发布时间】:2017-01-06 12:22:59
【问题描述】:
我正在研究 Angular 2 技术,在我当前的一个项目中,我遇到了使用打字稿代码的 HTTP 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);
}
}
当我调试我的代码时,一旦点击保存按钮,根据我的帖子网址,此方法将触发 public async Task Post(VehicleInfoTable vehicleInfo) { …… } 但是在那个 vehicleInfo 参数包含所有字段的值将是 null 但是当我发出发布请求时,我在正文中添加了数据。
您能告诉我在 http post 请求中将数据添加到正文中出错的地方吗?
你能告诉我我在哪里做错了吗?我该如何解决这个问题?
-Pradeep
【问题讨论】:
-
post请求失败时,你收到的错误是什么?
-
谢谢,你能不能看一下我更新的问题。
标签: angular http-post typescript2.0