我已经在stackoverflow上查看了以下问题,但我没有尝试做什么.
Ajax Authorization Request headers fails again and again
jQuery Ajax Unauthorized 401 Error
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);
}
});
});
我拥有的子域名受.htpasswd文件的密码保护.使用base64编码中使用的用户名/密码组合,网站的登录对我来说很好.
我在不同的域上运行此脚本而不是url所在的域,这就是为什么我有jsonp =?在网址中
从浏览器中的控制台获取的响应是:
获取http://sample.domain.com/script.PHP?name1=value1\u0026amp;jsonp=jsonp1334177732136 401(需要授权)