简体   繁体   中英

Authentification error on a XMLHttpRequest

I'm trying to get a JSON string given by http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/people

So I wrote this code :

<p id="liste"></p>

   <script>

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

    </script> 

But this gives me a 401 error for authentification. Besides, when I go manually on the link I get the normal login pop-up on which I can log with admin/admin and get the expected string.

Here are the two the console errors I get because of my XMLHttpRequest :

localhost: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.

For the moment, to continue my development, I copied manually the string in my code, because I can't find where the problem comes from but it's not really a solution... So if anyone knows how I can solve my problem I couldn't thank him/her enough !

You will find the documentation for the error here if you wan't more details.

PS : this is on a page (the .ftl) I created for Alfresco Share. I have an alfresco instance generated with Maven running on port 8080 on this instance of Share also generated with maven running on 8081. So maybe I have to add something in the xml that define the page ?

If you are within share page then with help of following,you can get all the users.

<script>
    Alfresco.util.Ajax.jsonGet({
        url : Alfresco.constants.PROXY_URI + "api/people",
        successCallback : {
            fn : function(res) {
                var results = Alfresco.util.parseJSON(res.serverResponse.responseText);
                console.log(results.people[0].firstName);
            },
            scope : this,

        },
        failureCallback : {
            fn : function() {
            },
            scope : this
        }
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM