我試圖從 API 下載 json 文件。為此,我需要調用 3 個端點。http://url.com/export它返回一個json: {"exportLoading":true,"file":"export-20190618-183316.json"}之后,我應該調用第二個端點并檢查此導出的狀態:http://url.com/export/status它返回trueor false(當服務器正在處理時,此端點返回true。當它返回false文件時,文件已完成下載。)因此,如果status === false, 我可以調用最后一個端點 http://url.com/download/file_name(我使此請求傳遞文件名 - 從第一個請求返回 - 以下載文件。我的問題是,如何檢查第二個端點是否返回false以發出最后一個請求并下載文件?我只是這樣做,直到第二個端點。app.get('/export', function (req, res, next) { global.fetch = fetch global.Headers = fetch.Headers; const headers = new Headers(); const username = 'user'; const password = 'pass'; const URL = 'http://url.com/export' headers.set('Authorization', 'Basic ' + base64.encode(username + ":" + password)); fetch(URL, { method: 'GET', headers: headers, }) .then(res => res.json()) .then(json => { fetch("http://url.com/exportl/status", { method: 'GET', headers: headers, }).then(result => ...) }) .catch(function (error) { console.log(error) }) });
如何檢查獲取響應以調用另一個響應?
呼啦一陣風
2021-07-02 10:03:16