Angular http使用RequestOptions失败

问题描述 投票:2回答:1

我有两个功能,一个工作另一个给我这个404错误:

/ get [object%20Object] 404(Not Found)

函数getNews给了我那个错误,我使用console.log查看两个函数的值,我得到相同的:this.newId =“1”,所以我用一个字符串调用这两个方法使用“新” “但它只适用于onVote方法。

为什么在getNews的情况下我在请求上得到了一个Object,而在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()
}
angular typescript
1个回答
3
投票

你得到的是使用+而不是,来添加选项:

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()
        })
}

应该解决这个问题

© www.soinside.com 2019 - 2024. All rights reserved.