我有這樣的代碼:無法通過Http POST方法中的數組值
空值傳遞數組對象。 當記錄細節時,它顯示正常。
投訴reg.component.html
<div class="col-md-6 panel panel-default">
<h2>Complaint registration</h2>
<form #complaintRegForm="ngForm" (ngSubmit)="createComplaint(complaintRegForm.value)">
<div class="form-group">
<label>District:</label>
<input type="text" class="form-control" name="District" ngModel>
</div>
<div class="form-group">
<label>Subject:</label>
<input type="text" class="form-control" name="Subject" ngModel>
</div>
<div class="form-group">
<label>Description:</label>
<input type="text" class="form-control" name="Description" ngModel>
</div>
<div class="form-group">
<label>Place of occurence:</label>
<input type="text" class="form-control" name="PlaceOfOccurence" ngModel>
</div>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" name="Name" ngModel>
</div>
<div class="form-group">
<label>Gender:</label>
<input type="radio" class="form-control" name="Male" ngModel>Male
<input type="radio" class="form-control" name="Female" ngModel>Female
</div>
<div class="form-group">
<label>Address:</label>
<input type="text" class="form-control" name="Address" ngModel>
</div>
<div class="form-group">
<label>Mobile no:</label>
<input type="number" class="form-control" name="MobileNo" ngModel>
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" class="form-control" name="Email" ngModel>
</div>
<div class="form-group">
<label>Date of occurence:</label>
<input type="text" class="form-control" name="DateOfOccurence" ngModel>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
投訴reg.component.ts
import { Component, OnInit } from '@angular/core';
import { ComplaintRegistrationService } from './complaint-registration.service';
@Component({
selector: 'app-complaint-registartion',
templateUrl: './complaint-registartion.component.html',
styleUrls: ['./complaint-registartion.component.css'],
providers:[ComplaintRegistrationService]
})
export class ComplaintRegistartionComponent implements OnInit {
constructor(private complaintCreation : ComplaintRegistrationService) { }
ngOnInit() {
}
complaintObj = {};
createComplaint(Obj){
console.log(Obj);
this.complaintCreation.createCompliants(Obj).subscribe();
}
}
和 投訴reg.service.ts
import { Injectable } from '@angular/core';
import { Http,Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class ComplaintRegistrationService{
private _url:string ="http://192.168.0.106:8000/app/complaint/create"
constructor(private _http:Http){}
createCompliants(complaintObj){
return this._http.post(this._url,complaintObj).map((response:Response)=>console.log(response));
}
}
傳遞空值。附加屏幕
您正在記錄響應,而不是請求,那麼您在後端實際收到了什麼?這似乎是屬性不匹配。你必須記住這是區分大小寫的,例如'District'與'District'不一樣 – Alex
@ AJT_82謝謝。我會嘗試 – Ajay
@ AJT_82謝謝Bro.it工作了很多 – Ajay