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

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.

    评论

报告相同问题?

悬赏问题

  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问
  • ¥15 用MATLAB汇总拟合图
  • ¥15 智能除草机器人方案设计
  • ¥15 对接wps协作接口实现消息发送
  • ¥15 SQLite 出现“Database is locked” 如何解决?