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