有人可以向我解釋,如果可以在AngularJS Promise中從Web API響應中獲取標題嗎?爲什麼AngularJS Promise無法從WEB API響應中檢索標題?
我提出以下電話:
return $http({
url: "http://localhost:19260/api/Authentication/Login/{credentials}",
method: "POST",
data: JSON.stringify(input),
dataType: "json"
}).then(function (response) {
if (response.status === 200) {
var jwt= response.headers('Authorization'); //Always null!!!!
auth = true;
}
else {
auth = false;
}
}, function (response) {
auth = false;
});
每次我嘗試通過「response.headers(......)」讀我的頭值爲null。爲什麼是這樣?當我看着小提琴手的迴應時,我可以看到標題及其價值。爲什麼AngularJS無法看到它?
這是CORS的問題嗎?客戶端/服務器都在本地主機上運行,但具有不同的端口。如果是這樣,我的CORS實施有什麼問題? (C#)
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class AuthenticationController : ApiController
{
[Route("api/Authentication/Login/{credentials}")]
[HttpPost]
public HttpResponseMessage Login([FromBody]CredentialContainer credentials)
{
var response = new HttpResponseMessage();
var jwt = GetJwtToken(credentials);
response.Headers.Add("Authorization", jwt);
response.StatusCode = HttpStatusCode.OK;
return response;
}
可能想看看這個問題:http://stackoverflow.com/q/27623389/215552。它不是重複的,但是在Web API中使用CORS提供了很好的建議。 –
global.asax中的enableCors()也是? – naveen
@naveen你的意思是在WebApiConfig.cs中? – Dave