AnsweredAssumed Answered

Get RESTApi response

Question asked by jackarnd on May 30, 2017
Latest reply on Jun 6, 2017 by jackarnd


Hello,
I'd like to parse a string given by : http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people but my code doesn't log in alfresco.
Thus, I wrote this code in my custom share page:

<p id="peoplelist"></p>

<script>
   
var people = new XMLHttpRequest();
   
people.onreadystatechange = function() {
      
if (this.readyState == 4 && this.status == 200) {
         
obj = JSON.parse(this.responseText);
         
document.getElementById("peoplelist").innerHTML = obj.list.entries[0].entry.firstName + obj.list.entries[0].entry.lastName + "," + obj.list.entries[0].entry.email;
      
}
   
};
   
people.open('GET', 'http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people'true, 'admin', 'admin');
   
people.send();
</script>

So, this gives me a basic 401 console error about the authentification

:8080/alfresco/api/-default-/public/alfresco/versions/1/people Failed to load resource: the server responded with a status of 401 (Unauthorized)
new-page:1 XMLHttpRequest cannot load http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8081' is therefore not allowed access. The response had HTTP status code 401.

Besides, when I go on http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people  the basic login pop-up opens in which I can log in with admin/admin and get the expected result:

{"list":{"pagination":{"count":7,"hasMoreItems":false,"totalItems":7,"skipCount":0,"maxItems":100},"entries":[{"entry":{"lastName":"Beecher","userStatus":"Helping to design the look and feel of the new web site","jobTitle":"Graphic Designer","statusUpdatedAt":"2011-02-15T20:20:13.432+0000","mobile":"0112211001100","emailNotificationsEnabled":true,"description":"Alice is a demo user for the sample Alfresco Team site.","telephone":"0112211001100","enabled":false,"firstName":"Alice","skypeId":"abeecher","avatarId":"198500fc-1e99-4f5f-8926-248cea433366","location":"Tilbury, UK","company":{"organization":"Moresby, Garland and Wedge","address1":"200 Butterwick Street","address2":"Tilbury","address3":"UK","postcode":"ALF1 SAM1"},"id":"abeecher","email":"abeecher@example.com"}},{"entry":{"firstName":"Administrator","emailNotificationsEnabled":true,"company":{},"id":"admin","enabled":true,"email":"admin@alfresco.com"}},{"entry":{"firstName":"Alex","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"alexandra","enabled":true,"email":"alexandra.lol@test.com"}},{"entry":{"firstName":"Guest","emailNotificationsEnabled":true,"company":{},"id":"guest","enabled":false}},{"entry":{"firstName":"Jack","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"jack","enabled":true,"email":"jack.lol@test.com"}},{"entry":{"lastName":"Jackson","userStatus":"Working on a new web design for the corporate site","jobTitle":"Web Site Manager","statusUpdatedAt":"2011-02-15T20:13:09.649+0000","mobile":"012211331100","emailNotificationsEnabled":true,"description":"Mike is a demo user for the sample Alfresco Team site.","telephone":"012211331100","enabled":false,"firstName":"Mike","skypeId":"mjackson","avatarId":"3fbde500-298b-4e80-ae50-e65a5cbc2c4d","location":"Threepwood, UK","company":{"organization":"Green Energy","address1":"100 Cavendish Street","address2":"Threepwood","address3":"UK","postcode":"ALF1 SAM1"},"id":"mjackson","email":"mjackson@example.com"}},{"entry":{"firstName":"Nicolas","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"nicolas","enabled":true,"email":"nicolas.lol@test.com"}}]}}

If anyone knows where is my mistake thanks in advance !

Outcomes