0
考虑下面的代码:jQuery的验证错误
var jsonData1 = $.ajax({
url: 'http://'+domainName+':9898/jolokia/read/*',
dataType:"json",
crossDomain: true,
beforeSend: function(xhr){
var username= '*';
var password= '*';
xhr.setRequestHeader("Authorization",
"Basic " + btoa(username + ":" + password));
},
async: false
}).responseText;
var parsed1 = JSON.parse(jsonData1);
这里的时候,我直接访问的URL,它要求输入用户名和密码,当给出显示值。 但是,当我做到这一点通过Ajax调用,它抛出这个错误:
Failed to load resource: the server responded with a status of 401 (Unauthorized)
jquery.min.js:5 XMLHttpRequest cannot load http://10.91.240.21:9898/jolokia/read/* No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401.
(index):1 Uncaught SyntaxError: Unexpected token u
TRY 2:
var jsonData2 = $.ajax({
type:'GET',
url: 'http://'+domainName+':9898/jolokia/read/*',
dataType:"jsonp",
crossDomain: true,
data: {username: '*',password: '*'},
async: false
}).responseText;
var parsed2 = JSON.parse(jsonData2);
我试着使用JSON页。 未经授权的代码得到解决。它显示状态正常。但现在这个错误来了
Uncaught SyntaxError: Unexpected token u
http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax – Cheery 2014-11-25 04:36:14
@Cheery我使用beforeSend说法。 – 2014-11-25 04:39:56
查看链接的第二个答案。现代jQuery拥有用户名和密码的属性。或者,至少,属性标题 – Cheery 2014-11-25 04:41:49