获取 Javascript 服务加载资源失败:服务器响应状态为 401(未授权)

Posted

技术标签:

【中文标题】获取 Javascript 服务加载资源失败:服务器响应状态为 401(未授权)【英文标题】:fetch Javascript service Failed to load resource: the server responded with a status of 401 (Unauthorized) 【发布时间】:2021-01-29 00:01:18 【问题描述】:

我尝试使用 javascript(特别是获取metode)创建服务,但出现此错误:加载资源失败:服务器响应状态为 401(未授权)。凭据很好,因为我已经在邮递员中对此进行了测试。

我有服务的网址:https://felipemoreno.co/server.html

我正在使用此代码:

<!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>

【问题讨论】:

【参考方案1】:

根据this answer,这是由于 no-cors 模式 - 浏览器在没有预检 OPTIONS 请求的情况下不支持跨域请求中的 Authorization 之类的标头。

【讨论】:

以上是关于获取 Javascript 服务加载资源失败:服务器响应状态为 401(未授权)的主要内容,如果未能解决你的问题,请参考以下文章

加载资源失败:服务器响应状态为 404(未找到)

Laravel pusher,/broadcasting/auth:1 加载资源失败:服务器响应状态为 500(内部服务器错误)

Angular 5 - 加载资源失败:服务器响应状态为 404(未找到)

bootstrap.min.css 加载资源失败:net::ERR_CONNECTION_REFUSED

“加载资源失败:服务器响应状态为 404”express docker

错误:加载资源失败:服务器响应状态为 404(未找到)