如何在 Angular 2 中正确设置 Http 请求标头

2023-12-04

我有一个使用 Angular 2 的 Ionic 2 应用程序,它将 Http PUT 发送到 ASP.NET Core API 服务器。这是我用来发送请求的方法:

public update(student: Student): Promise<Student>
{
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('authentication', `${student.token}`);

    const url = `${this.studentsUrl}`;

    return this.http
        .put(url, JSON.stringify(student), { headers: headers })
        .toPromise()
        .then(() => student)
        .catch(this.handleError);
}

我正在标头对象上设置身份验证键/值。

但是当我在服务器上收到此请求时,我在标头中找不到身份验证密钥:

enter image description here

如图所示,标头上有很多密钥,但没有我在客户端应用程序中手动添加到标头中的内容和身份验证密钥。

我究竟做错了什么?


http.put() 中请求选项的参数实际上应该是 RequestOptions 类型。尝试这样的事情:

let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('authentication', `${student.token}`);

let options = new RequestOptions({ headers: headers });
return this.http
    .put(url, JSON.stringify(student), options)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Angular 2 中正确设置 Http 请求标头 的相关文章

随机推荐