我有一些代码可以发布一些数据来创建数据记录。
它在一个服务中:
这是代码:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private http: HttpClient) { }
create() {
const postedData = { userid: 1, title: 'title here', body: 'body text' };
return this.http.post('https://jsonplaceholder.typicode.com/posts', postedData, httpOptions).subscribe(result => {
console.log(result);
}, error => console.log('There was an error: '));
}
}
我的问题是...我该怎么做,URL 要求用户登录...我如何传递凭据?
为了使用 cookie 进行请求,您需要添加带有凭据在你的要求中。看下面的代码
const httpOptions = {
headers: new HttpHeaders({
'Authorization': fooBarToken
}),
withCredentials: true
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)