【发布时间】: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>
【问题讨论】:
标签: javascript rest service fetch