【发布时间】:2016-04-25 16:12:41
【问题描述】:
我正在使用 fetch API 在我的 React Native 应用程序中调用查询服务器。但是,我的应用程序在收到服务器的响应后需要 50 秒来调用 then 函数。我做错了什么还是 Promise 工作很慢?
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
body: bodyContent
}.then((responseText) => {
console.log(responseText);
responseText.json().then(function(response){
console.log(response);
});
});
response 在responseText 之后 50 秒后在日志中打印
更新:刚刚发现responseText.json() 承诺只有在我再次点击屏幕后才会执行。这个问题很奇怪。
【问题讨论】:
-
您在查看请求后发现问题出在哪里?
-
我看到了控制台日志。它首先记录 responseText 并在 50 分钟后记录响应。
-
那么请求需要这么长时间,网络面板会向您显示一些详细信息,但您可能需要查看服务器端日志以了解问题所在。如果问题出在响应回调中,那么返回的响应有多大,需要永远解析?
-
你指的控制台日志是上面代码中的console.log(responseText)吗?那么cuplrit不是json函数吗?
-
服务器立即返回数据。 responseText 来自服务器。 json 函数需要 50 秒才能从
responseText中提取response
标签: javascript react-native fetch-api