簡體   English   中英

Node.js fetch 返回空數據

[英]Node.js fetch returns empty data

我正在嘗試使用fetch從 API 獲取一些數據,並且控制台日志顯示[] ,但是,當我使用 Postman 執行相同的請求時,它成功返回了 Z0ECD11C1D7A287401D148A23BBD7A2。 我究竟做錯了什么?

let fetch = require("node-fetch");

const url_deals = 'http://www.cheapshark.com/api/1.0/deals';

function getDealInfo(dealID){
    let new_url = new URL(url_deals + "?id=" + dealID);

    fetch(new_url, {method : 'GET'})
        .then(data =>{
            data.json().then(out => {
                console.log(out);
            })
        })
}

getDealInfo("X8sebHhbc1Ga0dTkgg59WgyM506af9oNZZJLU9uSrX8%253D");

郵遞員截圖

您的 id 顯然被多次編碼。 如果我將它解碼兩次,我會得到:

getDealInfo("X8sebHhbc1Ga0dTkgg59WgyM506af9oNZZJLU9uSrX8=");

而且,這實際上獲取了這些數據。

{ gameInfo:
   { storeID: '1',
     gameID: '93503',
     name: 'BioShock Infinite',
     steamAppID: '8870',
     salePrice: '29.99',
     retailPrice: '29.99',
     steamRatingText: 'Overwhelmingly Positive',
     steamRatingPercent: '95',
     steamRatingCount: '61901',
     metacriticScore: '94',
     metacriticLink: '/game/pc/bioshock-infinite',
     releaseDate: 1364169600,
     publisher: 'N/A',
     steamworks: '1',
     thumb:
      'https://steamcdn-a.akamaihd.net/steam/apps/8870/capsule_sm_120.jpg?t=1568739647' },
  cheaperStores:
   [ { dealID: 'fq0cNHiR3Z4TpZyV7WH865C1%2BCBlmufYUc%2Bu2HqyUHE%3D',
       storeID: '27',
       salePrice: '26.99',
       retailPrice: '29.99' } ],
  cheapestPrice: { price: '5.27', date: 1543553226 } }

因此,您傳遞的 id 值似乎不正確。 You can see that the node-fetch() code is working fine by just using it with the base URL: http://www.cheapshark.com/api/1.0/deals and that will return you JSON, confirming that the node-fetch()代碼工作得很好,問題在於您的id值。

問題出在你的身份證上。 你已經逃脫了兩次。

getDealInfo("X8sebHhbc1Ga0dTkgg59WgyM506af9oNZZJLU9uSrX8%253D");

您的代碼中的 ID 是X8sebHhbc1Ga0dTkgg59WgyM506af9oNZZJLU9uSrX8%253D但在屏幕截圖中它是X8sebHhbc1Ga0dTkgg59WgyM506af9oNZZJLU9uSrX8%3D 查看倒數第三個數字。

使用該 ID 我也一無所獲。

請注意,如果您在沒有id的情況下發出請求,則 ID 與您的格式不匹配,例如,

"dealID":"tnXImZO%2FYSnkve2w7O5fHy77qn0e8uvWFbkiT%2Fjb7r0%3D"

看起來您在某處進行了額外的 URI 編碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM