获得状态-1而不是401 Angularjs [英] getting status -1 instead 401 Angularjs
问题描述
我正在尝试从服务器获得响应。该函数看起来如此:
I'm trying to get a response from server. The function looks so:
function getOverview() {
var req = {
method: 'GET',
url: base,
headers: {
'authorization': 'Bearer ' + GottenTokens.getSessionToken()
}
};
return $http(req).then(
function successCallback(response) {
return response;
}, function errorCallback(response) {
console.log(response);
return response;
});
}
当状态为200时,没有问题,它会返回良好的响应状态200.但是......当状态为401时,它返回状态为-1的响应:
When the status is 200, there's no problem, it returns the good response with status 200. But... when the status is 401, than it returns a response with status -1:
Object {data: null, status: -1, config: Object, statusText: ""}
我试过了在postman中,我看到状态为401,并且在浏览器中也是这样说的:
I was trying it in postman and I saw there's the status 401 and also in browser it says so:
XMLHttpRequest cannot load http://localhost:5000/overview. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 401.
这意味着浏览器和邮递员从服务器获取状态,但它不会出现在Angularjs中。我正在尝试这篇文章:捕获Http 401 但它没有帮我。我在工厂中有这个功能。
It means that the browser and postman take the status from server, but it doesn't come in Angularjs. I was trying this post: Capture Http 401 but it doesn't help me. I have that function in a "factory".
顺便说一下,服务器是用akka编写的。我不知道它是否有事可做。以前在NodeJS中,我没有遇到任何问题。因为它在akka我无法获得Angularjs的状态。
By the way, the server is written in akka. I don't know if it has something to do. Before was it in NodeJS and I didn't have problems. Since it's in akka I can't get the status in Angularjs.
有人可以帮帮我吗?
推荐答案
在进行跨站点请求时,服务器必须向ALL响应添加有效的CORS头。这包括401之类的错误响应。
When doing cross-site requests, the server has to add to ALL responses a valid CORS header. This includes error responses like 401.
确保您可以看到Access-Control-Allow-Origin
401回复中的标题,很可能丢失。
Make sure you can see the "Access-Control-Allow-Origin"
header in your 401 response, it's most likely missing.
这篇关于获得状态-1而不是401 Angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!