简体   繁体   English

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

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

I am using flowroute api and need to fetch data but not sure how to add authentication credentials ie can be seen when I pass the query url in the browser How can I pass it in the javascript. 我正在使用flowroute api,并且需要获取数据,但不确定如何添加身份验证凭据,即当我在浏览器中传递查询URL时可以看到如何在javascript中传递它。 I am using wix platform and adding javascript code as given below 我正在使用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...



});

what is the correct method to pass username and password to the API so I can get a json response instead of current 401 Unauthorized response status 什么是将用户名和密码传递给API的正确方法,以便我可以获取json响应而不是当前的401未经授权的响应状态

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

Updating my Answer to show you the Network Call Details Request & Response headers ae as given bwlow in the screenshot 更新我的答案以显示屏幕截图中给定的网络呼叫详细信息请求和响应标头ae 在此处输入图片说明 在此处输入图片说明

在此处输入图片说明

Try: 尝试:

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.

相关问题 如何使用 javascript 将用户名和密码从页面传递到另一个页面 - how to pass username and password from page to another using javascript 如何在从 API 获取数据时打印子文件夹数据 - How to print subfolder data while fetching data from API 控制器代码从API获取数据时,如何向用户显示“获取数据”文本? - How to display “Fetching data” text to user while controller code is fetching data from API? 从 Angular 中的 API 获取数据时出现问题 - Trouble while fetching data from API in Angular 如何从Ajax中的任何Api提取数据时计算提取时间 - How to calculate Fetching Time while fetching data from any Api in Ajax 如何将用户名和密码传递给URL? - how to pass username and password to URL? 使用标头(包括GUID用户名和密码)从API获取数据 - Fetch data from API using headers including GUID Username and Password 如何正确地将用户名和密码从reactjs传递到后端进行身份验证? - How to properly pass username and password from reactjs to back end for authentication? 将用户名/密码安全地传递到另一个JavaScript文件 - Pass username/password to another javascript file safely 如何从 JavaScript 中受密码保护的 API 中获取数据? - How to fetch data from password protected API in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM