weixin_33704234 2017-01-10 21:19 采纳率: 0%
浏览 202

window.fetch .then(),不等待

Using window.fetch() in Firefox and Chrome to get data from a local JSON file, is proving troublesome:

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);    
});

For whatever reason, the first .then() function is being called prior to the full AJAX response, resulting in promise object (response) being <state>: "pending"

Which leads to an unwanted output as I'm not getting the data I wanted to get.

I've looked at multiple documents on I can't seem to find anything on my end that I'm doing incorrectly.

Any ideas?

</div>
  • 写回答

2条回答 默认 最新

  • ℙℕℤℝ 2017-01-10 21:40
    关注

    After a bit more Googling, I found this article by Google: https://developers.google.com/web/updates/2015/03/introduction-to-fetch#chaining-promises

    Looks like you have to build some extra functions to wait for the status to be resolved. This will then pass the response properly, as you'd expect.

    评论

报告相同问题?