0

I've tried make a service using javascript (specifically fetch metode) but appear this error: Failed to load resource: the server responded with a status of 401 (Unauthorized). The credentials are fine because I've test this in postman.

enter image description here

Url where I have the service: https://felipemoreno.co/server.html

I'm using this code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>


    <script type="text/javascript">

    console.log("prueba 352");

    var markers = {}; //create obj
    var markersss_array = []
    markersss_array.push("1019035955");
    //more datas push in array
    markers["identificacion"] = markersss_array; //pass aray to json object

    var markersConvertido = JSON.stringify(markers);

    var url = 'https://osb.urosario.edu.co/uxxi-URO/WsFotografias/proxy/AdministradorFotografiasJsonPS/fotos/consultar';
    var username = 'USERNAME';
    var password = 'PASSWORD';

    let headers = new Headers();

    //headers.append('Content-Type', 'text/json');
    headers.set('Authorization', 'Basic ' + btoa(username + ":" + password));
    headers.set('Content-type', 'application/json; charset=utf-8');

    fetch(url, {method:'POST',
            mode: 'no-cors',
            headers: headers,
            body: markersConvertido

    })
    .then(response => {
        if (!response.ok) throw new Error(response.status);
            return response.json();
    });


</script>

</body>
</html>
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
Felipe Moreno
  • 81
  • 3
  • 9

1 Answers1

0

According to this answer that's due to the no-cors mode – browsers don't support headers like Authorization in cross-origin requests without having a preflight OPTIONS request.

mweber
  • 670
  • 7
  • 18