使用 RequestOptions Angular http 失败

标签 angular typescript

我有两个函数,一个可以工作,另一个给我这个 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/

相关文章:

javascript - (变化)与(ngModelChange) Angular

javascript - WEBPACK_IMPORTED_MODULE_13___default(...) 不是函数

Angular 为 :host in ngOnInit 设置 css 样式

angular - 如何使用 switchMap 而不是嵌套订阅?

Angular 8 - POST + 重定向与提交 HTML <form> 完全一样,但不使用 DOM

html - 在 Angular 模板绑定(bind)中剥离 html

angular - 使用带有嵌套 ngfor 的模板

Angular Sidebar Treeview 没有响应

javascript - TypeORM 与除 id 之外的字段的关系

typescript - 如何在 TypeScript 中为 'Date' 数据类型创建扩展方法