这是向我的服务器发送http发布请求的功能
likePost(post){
let head = new Headers({'Content-Type': 'application/json',
"Authorization":"JWT "+localStorage.getItem("token")
});
let requestOptions = new RequestOptions({headers: head});
this.http.post(this.likeurl+post.id+'/like',requestOptions)
.subscribe((response)=>{
console.log(response);
});
}
但是每当我触发此功能时,我都会收到服务器发出的403禁止请求,说您未经身份验证
这个问题太有趣了,因为我所有的操作都使用发送到服务器的相同标头完全正常 但是此功能无法向服务器发出请求
这是我在服务器中创建对象的要求
createPost(input:HTMLInputElement){
// input.value='';
let post={content:input.value};
let head = new Headers({ 'Content-Type': 'application/json',
"Authorization":"JWT "+localStorage.getItem("token")
});
let requestOptions = new RequestOptions({headers: head});
let body = JSON.stringify(post);
this.http.post(this.url,body,requestOptions)
.subscribe(response =>{
post['id']=response.json().id;
this.posts.splice(0,0,post);
});
}
那样,所有的操作操作都与我的授权标头完全同步,但是这种功能并不是 我不明白问题是什么以及如何解决 试图观察我是否编写了错误的代码,但是一切似乎都很好
答案 0 :(得分:0)
您可以检查的几件事:
还要考虑一件事,您应该使用Angular Interceptor来做到这一点。
您将在此处复制代码,并在将要执行的每个http请求方法上添加标头。 This应该可以帮助您。