【问题标题】:HTML, Javascript: When doing fetch from ONCLICK error: NetworkError when attempting to fetch resourceHTML,Javascript:从 ONCLICK 获取时出错:尝试获取资源时出现 NetworkError
【发布时间】:2021-06-07 16:03:39
【问题描述】:

这令人困惑。

我在 Firefox 控制台中收到此错误:

Uncaught (in promise) TypeError: NetworkError when trying to fetch resource.

调用此函数时:

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 上我没有收到任何错误,但也没有结果。

这是我的错吗?

【问题讨论】:

标签: javascript


【解决方案1】:

答案是从 HTML 发布事件:

事件 = 事件 ||窗口.事件; event.preventDefault();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 2018-07-13
    • 2021-07-30
    • 2022-06-30
    • 2015-05-10
    • 2020-05-15
    • 2019-01-13
    相关资源
    最近更新 更多