首页 > 解决方案 > Fetch URL 调用显示 Promise {: “待办的” }

问题描述

为什么这个 url fetch 不起作用?

实际上,这个 GET 方法正在传递一些要存储的错误消息:

fetch('http://www.govtschemes.in/pushlo90.php?msg=alert-bid:0.120148336001477231576473857578-Please%20enter%20correct%20captcha', {
    method: 'get'
}).then(function(response) {

}).catch(function(err) {
    // Error :(
});

但是,如果我在浏览器中输入相同的 URL(http://www.govtschemes.in/pushlo90.php?msg=alert-bid:0.120148336001477231576473857578-Please%20enter%20correct%20captcha)它可以工作。

WebExension 中的其他一些地方工作正常。

但不在其他地方工作。此外,当我进入 Firefox 控制台时,它也不起作用。它显示了一些“待定..”

此函数也显示相同的行为:

function ff_httpGetAsync(theUrl, callback, failed_cb) {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
      // console.log("Successfully downloaded the ajax page");
      if (callback) {
        if (xmlHttp.responseURL == theUrl) {
          callback(xmlHttp.response);
        } else {
          console.log("diff response url received" + xmlHttp.responseURL);
        }
      }
    } else {
      // console.log("Got status =", xmlHttp.status);
    }
  }

  xmlHttp.open("GET", theUrl, true); // true for asynchronous 
  console.log("Gettiy :" + theUrl);
  xmlHttp.send(null);
}

ff_httpGetAsync('http://www.govtschemes.in/pushlo90.php?msg=alert-bid:0.120148336001477231576473857578-Please%20enter%20correct%20captcha', function() {

}, function() {});

我检查了服务器。在这种情况下,不会调用后端 pushlo90.php。

不确定我的网址有什么问题?

标签: javascriptfirefoxget

解决方案


该结果告诉您承诺尚未得到答复。在呈现页面之前,它可能在某些情况下非常快速地处理承诺。

使用承诺,您基本上会说“向我保证你会这样做”。这个承诺要么被解决,要么被拒绝。在解决或拒绝之前,它始终处于待处理状态。

在您的第一个函数中添加一些日志记录应该可以解释。

fetch('http://www.govtschemes.in/pushlo90.php?msg=alert-bid:0.120148336001477231576473857578-Please%20enter%20correct%20captcha', {
    method: 'get'
}).then(function(response) {
    console.log(response) //do something with response data the promise gives as result
}).catch(function(err) {
    console.log(err)// Error :(
});

如果您不想使用 .then(),请使用 async/await。

const functionName = async () => {
    const result = await fetch(
        "http://www.govtschemes.in/pushlo90.php?msg=alert-bid:0.120148336001477231576473857578-Please%20enter%20correct%20captcha",
        {
            method: "get"
        }
    );
    console.log(result); //this will only be done after the await section, since the function is defined as async
};

functionName();

推荐阅读