我有两个函数,一个可以工作,另一个给我这个 404 错误:
/get[object%20Object] 404 (Not Found)
函数 getNews 给了我这个错误,我使用 console.log 来查看两个函数的值,并且得到相同的结果: this.newId = "1",所以我使用 id 的字符串调用这两个方法“New”,但它只适用于 onVote 方法。
为什么在 getNews 案例中我在请求中得到了一个对象,而在 onVote 案例中我得到了 id?
这是 component.ts 文件:
export class NewDetailsComponent implements OnInit
{
params = new URLSearchParams();
options = new RequestOptions({ withCredentials: true });
oneNew: New;
newId: string;
constructor(public http: Http, private activatedRoute: ActivatedRoute)
{ }
ngOnInit()
{
this.activatedRoute.params.subscribe(params =>
{
this.newId = params['id']
});
this.getNews(this.newId)
}
getNews(id: string)
{
this.params.set('id', id)
this.options.search = this.params
this.http
.get(ApiConfig.API_URL + 'news/get' + this.options)
.toPromise()
.then(response =>
{
this.oneNew = <New>response.json()
})
}
onVote(id: string)
{
this.params.set('id', id.toString())
this.options.search = this.params
this.http
.get(ApiConfig.API_URL + 'news/Vote', this.options)
.toPromise()
}
最佳答案
您的 get 使用 +
而不是 ,
来添加选项:
getNews(id: string)
{
this.params.set('id', id)
this.options.search = this.params
this.http
.get(ApiConfig.API_URL + 'news/get', this.options)
.toPromise()
.then(response =>
{
this.oneNew = <New>response.json()
})
}
应该解决这个问题
关于使用 RequestOptions Angular http 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46516220/