这令人困惑。
我在 Firefox 控制台中收到此错误:
未捕获(承诺)类型错误:尝试获取资源时出现网络错误。
调用此函数时:
function sendMSG() {
let functionName = "getAllUsers"
let url = "https://api.kumulos.com/b2.2/asd/"+functionName+".json";
let username = 'asd';
let password = 'dsa';
let headers = new Headers();
headers.append('Content-Type', 'text/json');
headers.append('Authorization', 'Basic ' + username + ":" + password);
fetch(url,
{method:'POST',
headers: {
'Authorization': 'Basic ' + btoa(username+ ":" + password),
},
})
.then(response => response.json())
.then(json => console.log(json));
}
来自 HTML:
<button onclick="sendMSG()">Click me</button>
当我从代码本身调用此消息时,它可以完美运行,但是一旦我尝试单击,我就会收到上述错误。
在 Chrome 上我没有收到错误,但也没有结果。
这是我的错吗?
答案 0 :(得分:0)
答案是从 HTML 发布事件:
事件=事件||窗口事件; event.preventDefault();