我在建议的可能重复的问题中尝试了答案,但他们没有改变结果。jQuery ajax和跨域(CORS)和基本身份验证
我一直在尝试从本地PC上的客户端(测试Chrome和IE)通过ajax POST到远程服务器的API,但没有成功。 Ajax返回状态为0的错误,并且服务器返回401. 没有基本身份验证,我确认它工作正常。但使用基本身份验证,它从来没有工作。
客户:
$.ajax({
type : 'POST',
url : 'http://api-url',
data : data,
success : function (response) {
console.log(response);
},
error : function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
},
beforeSend : function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic base64username:password');
},
xhrFields : {
withCredentials : true
}
服务器:
header('Access-Control-Allow-Origin: http://localhost');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD, OPTIONS');
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization');
echo "ok";
任何人都可以提出什么我可能会丢失。我花了一天的时间,但找不到任何工作解决方案。
服务器是CentOS 6.7和Apache 2.2.15。
2016-01-26
mmrn
+1
的可能的复制[如何使用基本身份验证和jQuery和Ajax(http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery- and-ajax) –
+0
你用base64编码了整个'username:password'字符串吗?还是只有用户名? –
+0
我在建议的可能重复问题中尝试了答案,但他们没有更改结果。 –