cors跨域和ajax跨域,jQuery ajax和跨域(CORS)和基本身份验证

我在建议的可能重复的问题中尝试了答案,但他们没有改变结果。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

我在建议的可能重复问题中尝试了答案,但他们没有更改结果。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值