簡體   English   中英

GET 請求通過 Axios 返回錯誤 404,但在 Postman 中有效?

[英]GET request returns Error 404 through Axios, but works in Postman?

我正在使用GiantBomb API開發移動應用程序。 我正在使用 React Native 和 Expo 進行開發。 我目前正在應用程序中的一個屏幕上工作,該屏幕采用從列表中選擇的游戲的唯一 GUID 並顯示相關信息。 它通過 Axios GET 請求獲取所需的數據。

應用程序中的早期屏幕通過相同的方法從 API 檢索不同的數據,沒有問題,並且當將應用程序中使用的相同請求放入 Postman 時 - 使用相同的 GUID - 我得到了我期望返回的數據。

出於某種原因,通過 Axios 發出的 GET 請求返回 404 錯誤,而通過 Postman 發出的相同 GET 請求為我提供了我需要的信息。

這是在應用程序中發出的 Axios 請求:

    axios({
      url: "https://www.giantbomb.com/api/game/"+{gameId}+"/?api_key=[API_KEY]",
      method: 'GET',
    })
      .then(response => {
          setgameData(response.data);
      })
      .catch(err => {
          console.error(err);
      });
  }

我知道用於顯示游戲數據的屏幕具有 GUID,因為它顯示在屏幕上。 當顯示的 GUID 輸入到 Postman 請求中時,我得到了我期望的數據。

為什么會這樣? 我該如何解決?

編輯:以下是應用程序為我提供的 Axios 響應:

- node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\axios\lib\core\settle.js:16:9 in settle
- node_modules\axios\lib\adapters\xhr.js:53:6 in handleLoad
- node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:592:4 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue

3030-16836

Request failed with status code 404
- node_modules\axios\lib\core\createError.js:15:17 in createError
- node_modules\axios\lib\core\settle.js:16:9 in settle
- node_modules\axios\lib\adapters\xhr.js:53:6 in handleLoad
- node_modules\event-target-shim\dist\event-target-shim.js:818:20 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:592:4 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:395:6 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:189:10 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:416:4 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:109:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:108:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue

正如評論者所指出的(感謝@MinusFour @MasoudTahmasebi @spijs),問題是一個簡單的語法錯誤,涉及錯位的{}。 感謝大家!

暫無
暫無

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

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