在javascript中从api获取数据时如何传递用户名和密码凭据?

har*_*hal 4 javascript authentication velo

我正在使用flowroute api,需要获取数据,但不确定如何添加身份验证凭据,即当我在浏览器中传递查询 url 时可以看到如何在 javascript 中传递它。我正在使用 wix 平台并添加如下所示的 javascript 代码

// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import {fetch} from 'wix-fetch';
$w.onReady(function () {
    fetch('https://api.flowroute.com/v2/numbers/available?starts_with=800&limit=3?',{method: 'get',auth:{user:'28288282', pass:'099299292991'}})
      .then( (httpResponse) => {
       console.log(httpResponse.ok);
    if (httpResponse.ok) {

      return httpResponse.json();
    } else {
      return Promise.reject("Fetch did not succeed");



  }
  } )
  .then(json => console.log(json))
  .catch(err => console.log(err));

    //TODO: write your page related code here...



});
Run Code Online (Sandbox Code Playgroud)

将用户名和密码传递给 API 的正确方法是什么,以便我可以获得 json 响应而不是当前的401 未授权响应状态

https://api.flowroute.com/v2/numbers/available?starts_with=800&limit=3 在此处输入图片说明

更新我的答案以向您显示网络呼叫详细信息请求和响应标头 ae,如屏幕截图中给定的 bwlow 在此处输入图片说明 在此处输入图片说明

在此处输入图片说明

aga*_*r85 5

尝试:

fetch('https://api.flowroute.com/v2/numbers/available?starts_with=800&limit=3?', {
  method: 'get',
  headers: {
    "Content-Type": "text/plain",
    'Authorization': 'Basic ' + btoa(username + ":" + password),
  },
})
Run Code Online (Sandbox Code Playgroud)