我試圖做一個http.get請求從本地目錄中獲取文件,此刻我只想要console.log api得到的響應正在請求。 getFile()
顯示console.log但調用http
,不確定在angular2中如何使用http
;我知道在angular1中使用.then
作爲迴應。angular2調用http.get請求打開文件
import { Http, HTTP_PROVIDERS } from '@angular/http';
getFile() {
this.http.get('//localhost:1337/getFile');
console.log('get me the file');
}
阿比端點:
app.get('/getFile', function(req, res) {
var contents = fs.readFileSync('journey.xml');
console.log('Reading File....' + contents);
res.send(contents);
});
我得到「與狀態未捕獲的響應:0網址:空」錯誤和GET http:// url/getFile空回覆 – nCore
如果您在新標籤中打開// localhost:1337/getFile,它會向您展示什麼? – dfsq
對不起,我的api服務器沒有運行。我在console.log中獲取數據,但是當我調用getFile()時,我怎麼才能打開/下載文件?如果我打開/ getFile在另一個選項卡下載文件。 – nCore