Javascript 获取URL调用显示承诺{<;state>;:";pending";}
为什么此url获取不起作用 实际上,此GET方法正在传递一些要存储的错误消息:Javascript 获取URL调用显示承诺{<;state>;:";pending";},javascript,firefox,get,Javascript,Firefox,Get,为什么此url获取不起作用 实际上,此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) { /
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(),它就会工作
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
不确定我的URL有什么问题?结果告诉您,承诺尚未兑现。在某些情况下,在页面呈现之前,承诺处理得非常快,它可能会起作用 用一个承诺,你基本上说‘答应我你会这么做’。这一承诺要么得到解决,要么遭到拒绝。在它被解决或拒绝之前,它总是挂起的 在第一个函数中添加一些日志应该可以解释这一点
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();
fetch函数返回一个承诺,解析后返回HTTP响应。然后,您可以访问HTTP响应示例:
fetch(`https://baconipsum.com/api/?type=all-meat¶s=2&start-with-lorem=1`)
.then(response => {
// HTTP response which needs to be parsed
return response.json()
})
// accessing the json
.then(json =>console.log(json))
因此,你必须问自己的问题是:电话返回的是什么??
还要注意404和其他HTML错误代码不会导致拒绝承诺,所以不要为catch而烦恼
有关更多详细信息,请参见因此,问题中显示的代码块为-
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 :(
});
所以,它说的是fetch模块将向请求中提供的URL发送一个GET请求,响应或错误将进入相应的链接函数
错误可能是404(未找到)或401(未经授权)等
要检查错误,请将一些日志记录到HTTP请求处理程序中
fetch('http://www.govtschemes.in/pushlo90.php?msg=alert-bid:0.120148336001477231576473857578-Please%20enter%20correct%20captcha', {
method: 'get'
}).then(function(response) {
console.log(`Response is {response}`)
}).catch(function(err) {
console.log(`Error is {err}`)
})
对于您的其他代码,这里是从您的代码返回的屏幕截图-
如果它明确指出404(未找到),那么您的代码将进入错误处理程序。您可能会运行到哪个站点,这取决于您尝试执行抓取操作,但我不想读取任何数据。它已经在很多地方起作用了。但在一个地方,当在Firefox控制台中输入时,它不工作运行
wait fetch(/*…*/)为我返回404。然而,使用CORS代理运行良好,我得到了“处理/var/www/govtschemes.in/logs/21-12-2019.log
”。试试,“https://cors-anywhere.herokuapp.com/{orignial URL}
”。是的,我认为这是由混合块内容造成的。我在浏览器中启用了混合内容后,它似乎工作正常()