症状。
一个工作的GET REQUEST。
客户端:
this.http.get(this.baseUrl + "ControllerName/MethodName/" + id, {
headers: new HttpHeaders({
"Content-Type": "application/json",
"Authorization": "Bearer " + token
})
}).subscribe(response => {
console.log(response);
}, err => {
console.log(err);
});
Server -side:
[HttpGet]
[Authorize]
public IActionResult MethodName(Guid id)
{
return Ok(JsonConvert.SerializeObject(id));
}
POST REQUEST WHICH RETURNS 401. 客户端:
客户端: 服务器端: A POST REQUEST WHICH RETURNS 401: Client-side:
this.http.post(this.baseUrl + "ControllerName/MethodName/" + id, {
headers: new HttpHeaders({
"Content-Type": "application/json",
"Authorization": "Bearer " + token
})
}).subscribe(response => {
console.log(response);
}, err => {
console.log(err);
});
服务器端。
[HttpPost]
[Authorize]
public IActionResult MethodName(Guid id)
{
return Ok(JsonConvert.SerializeObject(id));
}
有谁知道为什么一个授权的GET请求可以工作,而一个同样的POST请求,对于同样的方法却不能工作?
在angular的post方法中,第一个参数是url,第二个参数是body,第三个参数是options。
this.http.post(this.baseUrl + "ControllerName/MethodName/" + id,null, {
headers: new HttpHeaders({
"Content-Type": "application/json",
"Authorization": "Bearer " + token
})
}).subscribe(response => {
console.log(response);
}, err => {
console.log(err);
});