我在 stackoverflow 上查看了以下问题,但我想做的事情却没有运气。
Ajax 授权请求标头一次又一次失败 https://stackoverflow.com/questions/9820170/ajax-authorization-request-headers-fails-again-and-again
jQuery Ajax 未经授权 401 错误 https://stackoverflow.com/questions/8847748/jquery-ajax-unauthorized-401-error
通过跨域帖子发送凭据? https://stackoverflow.com/questions/2054316/jquery-sending-credentials-with-cross-domain-posts
这是我目前拥有的代码:
$(document).ready(function() {
$.ajax({
url: 'http://sample.domain.com/script.php?name1=value1&jsonp=?',
type: 'GET',
dataType: 'json',
contentType: "application/json",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authentication", "Basic ZnJvbWFwcGx********uOnRoM24zcmQ1UmgzcjM=") //Some characters have been replaced for security but this is a true BASE64 of "username:password"
},
success: function(data){
alert(data);
}
});
});
</script>
我的子域受 .htpasswd 文件的密码保护。使用 Base64 编码中使用的用户名/密码组合,该网站的登录对我来说效果很好。
我在与 url 所在域不同的域上运行此脚本,这就是为什么我有 jsonp=?在网址中
我从浏览器控制台得到的响应是:GET http://sample.domain.com/script.php?name1=value1&jsonp=jsonp1334177732136 401 (Authorization Required)