我已经制作了一个API应用程序,并使用jQuery ajax我试图从API获取数据。但我收到错误401:授权错误。后来我发现标题授权没有传递请求,因此API无法跟踪它。下面是我用于我的应用程序的代码。请帮帮我......
PHP代码:
header("Access-Control-Allow-Orgin: *");
header("Access-Control-Allow-Methods: *");
header("Access-Control-Allow-Headers: Authorization, X-Requested-With, Content-Type");
header("Access-Control-Allow-Credentials: true");
jQuery代码:
$.ajax({
type: method,
url: url,
dataType: 'JSONP',
crossDomain: true,
headers: {'X-Requested-With': 'XMLHttpRequest'},
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Basic xxxx");
},
success: function (){
alert('Thanks for your comment!');
}
});