';错误请求';在JavaScript中使用Fetch方法调用API时

';错误请求';在JavaScript中使用Fetch方法调用API时,javascript,api,web,Javascript,Api,Web,我试图使用Fetch方法从API返回一些数据,但得到一个400错误。我可能错过了一些明显的东西,但我已经做了这么长时间了,我可能看不到树木的树木 <script type="text/javascript"> function getJSON() { const API = 'https://API-URL-ENDPOINT'; const QUERY = 'sample-query'; const A_KEY = '{API Key}'; const

我试图使用Fetch方法从API返回一些数据,但得到一个400错误。我可能错过了一些明显的东西,但我已经做了这么长时间了,我可能看不到树木的树木

<script type="text/javascript">
function getJSON() {
    const API = 'https://API-URL-ENDPOINT';
    const QUERY = 'sample-query';
    const A_KEY = '{API Key}';
    const A_SEC = '{API Secret}';

    let headers = new Headers();
    headers.append('Authorization', 'Basic ' + USERNAME + ":" + PASSWORD);

    {/* Fetch Method Here */}
      fetch(API, {method: 'GET', headers: headers })
      .then(response => response.json())
      .then(json => console.log(json));
      }

      function parseJSON(response) {
          return response.json()
          }
</script>

函数getJSON(){
常数API=https://API-URL-ENDPOINT';
常量查询='示例查询';
常量A_KEY='{API KEY}';
常数A_SEC='{API Secret}';
let headers=新的headers();
headers.append('Authorization','Basic'+用户名+“:“+密码);
{/*在此处获取方法*/}
获取(API,{method'GET',headers:headers})
.then(response=>response.json())
.then(json=>console.log(json));
}
函数parseJSON(响应){
返回response.json()
}

如果有人能提供任何提示,那就太好了。

正如授权标题文档中所述,您需要将其编码为base64


因此,尝试
headers.append('Authorization','Basic'+btoa(USERNAME+“:“+PASSWORD))

谢谢,已修复。如果你赶时间的话,打字错误甚至可以潜入基本问题中