【发布时间】:2019-01-19 16:37:34
【问题描述】:
这是一个向我的服务器发送 http post 请求的函数
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 禁止请求,说你没有经过身份验证
这个问题太有趣了,因为我所有的 crud 操作都可以正常工作,并且将相同的标头发送到服务器 但此函数无法向服务器发出请求
这是我在服务器中创建对象的帖子要求
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);
});
}
这样,所有的 crud 操作都与我的授权标头完全同步,但这种类似的功能不是 我不明白问题是什么以及如何解决 试图观察我是否编码错误,但一切似乎都很好
【问题讨论】:
标签: angular http-headers authorization jwt