繁体   English   中英

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

[英]How to pass username and password credentials while fetching data from api in javascript?

我正在使用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...



});

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

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

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

在此处输入图片说明

尝试:

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),
  },
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM