Angular Htpp Post 到 Json 显示 [object Object]
Angular Htpp Post to Json shows [object Object]
我的个人项目需要帮助。我正在尝试使用 Angular HTTP post 方法然后我遇到了这种错误。当我添加新数据时,调用 addStudent
,然后重新加载页面,semester
列 return/show [object Object]
。除了 semester
.
之外的其他列均正常工作
//file name: app.service.ts
addStudent(newId: string, newName: string, newYear: string, newSemester: string, newScore: string): Observable<any>
{
return this.http.post("http://localhost:3000/Student", {id: newId, name: newName, year: newYear, semester: newSemester, score: newScore});
}
//file name : app.component.ts
addStudent(newId: string, newName: string, newYear: string, newSemester: string, newScore: string)
{
this.stockService.addStudent(newId, newName, newYear, newSemester, newScore).subscribe();
console.log(newSemester);
}
<!--file name: app.component.html-->
<tr *ngFor="let Student of Student" [attr.id]="Student.id">
<td>{{Student.id}}</td>
<td>{{Student.name}}</td>
<td>{{Student.year}}</td>
<td>{{Student.semester}}</td>
<td>{{Student.score}}</td>
</tr>
</table>
</h1>
<h3>ADD NEW</h3>
<br>ID: <input #newId/>
<br>Name: <input #newName/>
<br>Year: <input #newYear/>
<br>Semester: <input #newSemester/>
<br>Score: <input #newScore/>
<br><button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester, newScore.value)">ADD</button>
添加新行后 Json 看起来像这样
{
"id": "a",
"name": "a",
"year": "a",
"semester": {},
"score": "a"
}
如果需要更多片段,请告诉我。
谢谢。
这只是错字。
<button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester, newScore.value)">ADD</button>
看到你忘记了新学期的 .value。
更改为:
<button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester.value, newScore.value)">ADD</button>
我的个人项目需要帮助。我正在尝试使用 Angular HTTP post 方法然后我遇到了这种错误。当我添加新数据时,调用 addStudent
,然后重新加载页面,semester
列 return/show [object Object]
。除了 semester
.
//file name: app.service.ts
addStudent(newId: string, newName: string, newYear: string, newSemester: string, newScore: string): Observable<any>
{
return this.http.post("http://localhost:3000/Student", {id: newId, name: newName, year: newYear, semester: newSemester, score: newScore});
}
//file name : app.component.ts
addStudent(newId: string, newName: string, newYear: string, newSemester: string, newScore: string)
{
this.stockService.addStudent(newId, newName, newYear, newSemester, newScore).subscribe();
console.log(newSemester);
}
<!--file name: app.component.html-->
<tr *ngFor="let Student of Student" [attr.id]="Student.id">
<td>{{Student.id}}</td>
<td>{{Student.name}}</td>
<td>{{Student.year}}</td>
<td>{{Student.semester}}</td>
<td>{{Student.score}}</td>
</tr>
</table>
</h1>
<h3>ADD NEW</h3>
<br>ID: <input #newId/>
<br>Name: <input #newName/>
<br>Year: <input #newYear/>
<br>Semester: <input #newSemester/>
<br>Score: <input #newScore/>
<br><button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester, newScore.value)">ADD</button>
添加新行后 Json 看起来像这样
{
"id": "a",
"name": "a",
"year": "a",
"semester": {},
"score": "a"
}
如果需要更多片段,请告诉我。
谢谢。
这只是错字。
<button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester, newScore.value)">ADD</button>
看到你忘记了新学期的 .value。
更改为:
<button type="button" role="button" (click)="addStudent(newId.value, newName.value, newYear.value, newSemester.value, newScore.value)">ADD</button>