XMLHttpRequest无法加载....预检响应具有无效的HTTP状态代码401 [英] XMLHttpRequest cannot load .... Response for preflight has invalid HTTP status code 401
问题描述
以下是我正在尝试进行基本身份验证的服务呼叫.我检查了多个博客都找不到解决方案.
Below is my service call where I am trying to do a basic auth. I have checked multiple blogs could not find the solution for this.
当我遇到错误时,有人可以帮助我解决此问题吗?
Can anybody help me to solve this issue as I am getting below error:
XMLHttpRequest无法加载
对预检的响应具有无效的HTTP状态代码401
Response for preflight has invalid HTTP status code 401
我也无法在开发人员选项的网络"标签中找到基本身份验证.
I could not find the basic auth in the network tab in developer options also.
function() {
"use strict";
var APIservice = function($http, $base64) {
var getDetails = function(postData) {
$http.defaults.headers.common['Access-Control-Allow-Origin'] = "*";
$http.defaults.headers.common['Access-Control-Allow-Methods'] = "GET,PUT,POST,DELETE,OPTIONS";
$http.defaults.headers.common['Access-Control-Allow-Headers'] = "Content-Type, Authorization, Content-Length, X-Requested-With";
$http.defaults.headers.common['Content-Type'] = undefined;
console.log($http.defaults.headers.common.Authorization);
//console.log($http.defaults.headers.common.Authorization);
return $http.get('http://52.74.68.202:8080/rest/v1/step/all')
.then(function(response, headers, config) {
console.log(response);
return response;
});
};
return {
getDetails: getDetails
}
}
var module = angular.module('expframework');
module.factory("APIservice", APIservice);
module.run(['$http', '$base64', function($http, $base64) {
var authdata = $base64.encode('test:test');
$http.defaults.headers.common['Authorization'] = 'Basic ' + authdata;
}]);
module.config(['$httpProvider', '$base64', function($httpProvider, $base64) {
var authdata = $base64.encode('test:test');
$httpProvider.defaults.headers.common['Authorization'] = 'Basic ' + authdata;
}])
}();
它可以在Safari和模拟器中使用,但不能在Chrome和Firefox中使用
It is working in Safari and emulators but not working in Chrome and Firefox
请帮助我解决此问题.预先感谢.
Please help me to fix this. Thanks in advance.
推荐答案
由于您的服务器抛出了401,所以我猜它试图对预检请求进行身份验证.来自 https://stackoverflow.com/a/15734032/1225328 :
Since your server threw a 401, I guess it tried to authenticate the preflight request. From https://stackoverflow.com/a/15734032/1225328:
针对CORS预检请求的W3规范明确指出应排除用户凭据.
The W3 spec for CORS preflight requests clearly states that user credentials should be excluded.
[...]
仅使服务器(在此示例中为API)无需身份验证即可响应OPTIONS
请求.
Simply have the server (API in this example) respond to OPTIONS
requests without requiring authentication.
这篇关于XMLHttpRequest无法加载....预检响应具有无效的HTTP状态代码401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!