我有一個我已經放在一起的小腳本。該腳本執行以下操作:
在陣列中定義多個變數
將這些值傳遞給 API
API 應回傳訪問令牌
const fetch = require('node-fetch'); var orgInfo = { client_id: 'idgoeshere', client_secret: 'secretgoeshere', username: 'usernamegoeshere', password: 'passwordgoeshere', grant_type: 'granttypegoeshere' }; fetch('https://urlgoeshere', { method: "GET", body: JSON.stringify(orgInfo), headers: { "Content-Type": "application/json" }, credentials: "include" }).then(function(response) { response.access_token response.bearer response.expires_in response.scope return repsonse.text() }, function(error) { error.message }) console.log(orgInfo); console.log(response.access_token);
當我登錄 orgInfo 時,我確實得到以下輸出:
{ client_id: 'idgoeshere',
client_secret: 'secretgoeshere',
username: 'usernamegoeshere',
password: 'passwordgoeshere',
grant_type: 'granttypegoeshere' }
當我嘗試記錄 response.access_token 時,出現ReferenceError: response is not defined
我的問題是:
- 是否需要定義回應?顯然,我被罵是因為它不是。
- 有沒有辦法查看我是否自動從 API 中得到任何東西?
我不是在找人用勺子喂我一個答案,而是我只是在尋找朝著正確方向前進的動力。那將是恒星。
謝謝
更新
所以這就是我所擁有的:
const fetch = require('node-fetch');
const orgInfo = {
client_id: ' ',
client_secret: ' ',
username: ' ',
password: ' ',
grant_type: ' '
};
(async() => {
const response = await fetch('https:// ', {
method: "GET",
body: JSON.stringify(orgInfo),
headers: {
"Content-Type": "application/json"
}
});
const data = await response.json();
console.log(data)
})
這在運行時不回傳錯誤,但也不回傳資料的值
uj5u.com熱心網友回復:
const fetch = require('node-fetch');
var orgInfo = {
client_id: 'idgoeshere',
client_secret: 'secretgoeshere',
username: 'usernamegoeshere',
password: 'passwordgoeshere',
grant_type: 'granttypegoeshere'
};
fetch('https://urlgoeshere', {
method: "GET",
body: JSON.stringify(orgInfo),
headers: {
"Content-Type": "application/json"
},
credentials: "include"
}).then(function(response) {
response.access_token
response.bearer
response.expires_in
response.scope
console.log(response.access_token);
return repsonse.text()
}, function(error) {
error.message
})
console.log(orgInfo);
response ins 作用域在 then 方法呼叫的函式內,所以它只能在這個函式內部訪問
uj5u.com熱心網友回復:
fetch回傳一個Promise物件。
APromise表示異步操作的最終完成(或失敗)及其結果值。這意味著response.access_token只保證在.then塊內有一個值(如果有的話),因為response只有在 promise 被實作時才會被評估。
您在控制臺中什么都沒有的原因是您在access_token不能保證具有值時嘗試訪問(因此console.log什么都不輸出 - 沒有什么可輸出)。
要解決此問題,您需要access_token在保證有回應時訪問該屬性。
那是在承諾完成之后,所以要么:
- 移動子句
console.log(response.access_token);內部.then
或者更清潔、更現代的解決方案是:
- 使用
await(等價的語法糖)
NBResponse物件是整個HTTP 回應的表示。
您正在使用response.text()which 將回應正文決議為string,而不是具有 properties的JS 物件。
我假設您想將Response物件中的正文內容作為 JSON決議為 JS 物件。在這種情況下,使用該json()方法將回傳第二個承諾,決議為從回應正文決議中獲得的 JavaScript 物件。
結果應該具有access_token您想要的屬性(考慮到 API 端點回傳它)。
這應該有效:
const response = await fetch('https://urlgoeshere', {
method: "GET",
body: JSON.stringify(orgInfo),
headers: {
"Content-Type": "application/json"
};
const data = await response.json();
console.log(data.access_token);
console.log(data.bearer);
console.log(data.expires_in);
console.log(data.scope);
...
uj5u.com熱心網友回復:
return repsonse.text() 應該是----> return response.text()
根據獲取檔案
- “Fetch API 的 Response 介面表示對請求的回應。您可以使用 Response.Response() 建構式創建一個新的 Response 物件,但您更有可能遇到作為另一個 API 操作的結果回傳的 Response 物件— 例如,服務作業者 Fetchevent.respondWith,或簡單的 fetch()。
對于您的問題“有沒有辦法查看我是否自動從 API 中得到任何東西?”
您可以嘗試使用console.log(response.status); 這將為您提供請求的狀態代碼。這些代碼可以在這里找到。以及這里使用的一個例子。
如果可以,我強烈建議嘗試使用 Postman 或 Thunder-client,這可以簡化所有這些并為您提供有關回應的所有資訊。測驗 API 呼叫并確切知道發生了什么非常有用。您還可以自動查看以其他語言撰寫的電話。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/336665.html
標籤:javascript 数组 json 回复 json响应
上一篇:Pangram檢測
下一篇:洗掉元素后如何回傳陣列?
