我有一个使用 Angular 2 的 Ionic 2 应用程序,它正在向 ASP.NET Core API 服务器发送一个 Http PUT。这是我用来发送请求的方法:
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);
}
我在 header 对象上设置身份验证键/值。
但是当我在服务器上收到这个请求时,我在 header 上找不到身份验证 key :
正如您在图片中看到的, header 上有很多 key ,但没有我在客户端应用程序中手动添加到 header 中的内容和身份验证 key 。
我做错了什么?
最佳答案
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)
关于http - 如何在 Angular 2 中正确设置 Http 请求 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41133705/