Angular http使用RequestOptions失败

时间:2017-10-01 19:43:59

标签: angular typescript

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

  

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

函数getNews给了我那个错误,我使用了console.log来查看两个函数的值,我得到了相同的结果:this.newId =" 1",所以我用两个方法调用使用"新"的ID的字符串但它只适用于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()
}

1 个答案:

答案 0 :(得分: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()
        })
}

应解决问题