Consider the following code:
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);
Here when I directly access the url, it asks for a username and password, which when given shows the value. But when I do it through an ajax call, it throws this error:
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);
I tried using json p. The unauthorized code got resolved. It is showing status OK. But now this error comes
Uncaught SyntaxError: Unexpected token u