angular 2 http删除参数url附加[object%20Object]
angular 2 http delete parameter url appended with [object%20Object]
我在 angular 2 中使用 http.delete。这是我的代码
app.component.ts
deleteCourse(id: string) {
console.log(id);
this.appService.deleteByID(id)
.subscribe(x => {console.log(x); this.fetchCourseData(); }
);
}
app.service.ts
deleteByID(id: string): Observable<string>
let options1 = new RequestOptions();
options1.headers = new Headers();
options1.headers.append('Content-Type', 'application/json');
return this.http.delete('http://localhost:3000/sample'+ '/' + id , options1)
.map((response: Response) => {
const result = response.json();
return result; });
}
db.json
{
"sample": [
{
{
"courseTitle": "ggg",
"courseDescription": "ggg",
"courseCredit": "2",
"id": 8
},
{
"courseTitle": "ggg",
"courseDescription": "ggg",
"courseCredit": "1",
"id": 9
},
{
"courseTitle": "add",
"courseDescription": "asdas",
"courseCredit": "2",
"id": 10
}
]
}
url 传递为
http://localhost:3000/sample/[object%20Object]
连同 404 error.How 我要纠正这个吗?
仔细检查 deleteCourse 中的 console.log 是否打印出字符串值。我设法重现了您的问题,方法是将一个对象作为参数传递给 deleteCourse
我在 angular 2 中使用 http.delete。这是我的代码
app.component.ts
deleteCourse(id: string) {
console.log(id);
this.appService.deleteByID(id)
.subscribe(x => {console.log(x); this.fetchCourseData(); }
);
}
app.service.ts
deleteByID(id: string): Observable<string>
let options1 = new RequestOptions();
options1.headers = new Headers();
options1.headers.append('Content-Type', 'application/json');
return this.http.delete('http://localhost:3000/sample'+ '/' + id , options1)
.map((response: Response) => {
const result = response.json();
return result; });
}
db.json
{
"sample": [
{
{
"courseTitle": "ggg",
"courseDescription": "ggg",
"courseCredit": "2",
"id": 8
},
{
"courseTitle": "ggg",
"courseDescription": "ggg",
"courseCredit": "1",
"id": 9
},
{
"courseTitle": "add",
"courseDescription": "asdas",
"courseCredit": "2",
"id": 10
}
]
}
url 传递为 http://localhost:3000/sample/[object%20Object]
连同 404 error.How 我要纠正这个吗?
仔细检查 deleteCourse 中的 console.log 是否打印出字符串值。我设法重现了您的问题,方法是将一个对象作为参数传递给 deleteCourse