【问题标题】:HTTP PUT header not working with Angular 2HTTP PUT 标头不适用于 Angular 2
【发布时间】:2017-08-20 17:21:21
【问题描述】:

我遇到了 Angular 2 的问题,确实,我需要发送带有 Authorization 标头的 PUT Http 请求,但事实并非如此工作。

但是它与 DELETEPOSTGET 完美配合(使用完全相同的代码)。

请求

private header = new Headers();
// [...]
launchPutRequest(): Promise<any> {
        const url = 'http://myurl.com/';
        this.header = new Headers();
        this.header.append('Authorization', 'Bearer ' + token);

        return this.http.put(url, { headers: this.header })
            .toPromise()
            .then(response => {
                return response.json().msg as any;
            });
    }

根据这张图片,我认为标题格式错误,但为什么只有 PUT

【问题讨论】:

    标签: angular http put


    【解决方案1】:

    您可能会遇到错误,因为put 请求的签名是:

    (url: string, body: any, option?: RequestOptionsArgs)
    

    您正在提供:

    (url, options)
    

    如果您不使用 body 参数,我建议您将 http 调用更改为:

    return this.http.put(url, null, { headers: this.header })
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 2018-04-13
      相关资源
      最近更新 更多