簡體   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