[英]ANGULAR isn't sending headers to my particular Http Post Request
這是用於將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);
});
}
這樣,所有的原始操作都與我的授權標頭完全同步,但是這樣的功能不是我不明白問題是什么以及如何解決這個問題,試圖觀察我是否編寫了錯誤的代碼,但是一切似乎都很好
您可以檢查的幾件事:
同樣要考慮的一件事,您應該使用Angular Interceptor做到這一點。
您將在此處復制代碼,並在將要執行的每個http請求方法上添加標頭。 這應該可以幫助您做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.