2017-01-10 29 views
0

在Firefox和Chrome使用window.fetch(),以獲得從本地JSON文件數據,證明是麻煩:window.fetch。那麼(),而不是等待

var url = "http://sandbox.ccchapel.com/Thy-Kingdom-Come/data/outreach-spree.json"; 
 
var request = new Request(url, { 
 
     method: 'get', 
 
     mode: 'no-cors' 
 
    }); 
 

 
fetch(request).then(function(response) { 
 
    console.log(response); 
 
    return response.json(); 
 
}).then(function(j) { 
 
    console.log(j);  
 
});

對於無論什麼原因,第一個.then()函數在完整的AJAX響應之前被調用,導致承諾對象(響應)爲

這導致不需要的輸出,因爲我沒有得到da我希望得到。

我看過多個文件,我似乎無法找到任何在我的結尾,我做錯了。

任何想法?

+0

您是在網站ccchapel.com上運行的?否則,您需要在服務器上支持跨源。查看[CORS-header'Access-Control-Allow-Origin']上的答案(http://stackoverflow.com/a/10636765/5459839) – trincot

+1

'response'不是承諾對象,您必須記錄其他內容。 – Bergi

回答

0

發佈的代碼很好,因爲它是。將網址替換爲

var url = 
    "data:application/octet-stream;base64,eyJncmVldGluZyI6ICJoZWxsbyBmb2xrcyJ9Cgo="; 

({「greeting」:「hello folks」})在行動中看到它。

似乎缺少的是承諾鏈上的.catch子句,用於報告.json文件中的解析錯誤。