Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获取http请求的内容?_Javascript_Node.js_Discord.js - Fatal编程技术网

Javascript 如何获取http请求的内容?

Javascript 如何获取http请求的内容?,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,所以我正在制作一个discord机器人,我想让它显示一些数据。所以我从一个网站上得到了我想要的内容,我使用了以下代码: const Discord=require('Discord.js'); var XMLHttpRequest=require(“XMLHttpRequest”).XMLHttpRequest; const{prefix,token}=require('./config.json'); const client=new Discord.client() 函数httpGet(th

所以我正在制作一个discord机器人,我想让它显示一些数据。所以我从一个网站上得到了我想要的内容,我使用了以下代码:

const Discord=require('Discord.js');
var XMLHttpRequest=require(“XMLHttpRequest”).XMLHttpRequest;
const{prefix,token}=require('./config.json');
const client=new Discord.client()
函数httpGet(theUrl)
{
var xmlHttp=new XMLHttpRequest()
open(“GET”,theUrl,false);//对于同步请求为false
xmlHttp.send(空);
返回xmlHttp.responseText;
}
client.on('ready',()=>{
log('Bot ready!');
})
client.on('message',message=>{
if(message.content.startsWith(`${prefix}http`){
让结果=httpGet('https://games.roblox.com/v1/games/2012508359/favorites/count')
message.channel.send(结果)
message.reply(`Still a WIP!${message.member.user.tag}`);
} 
})
client.login(令牌);
它可以工作,但显示如下:

{"favoritesCount":31219}
如何使它只显示数字(31219),而不显示
{“FavoriteSunt”:31219}


谢谢

您不需要传递
结果
而是需要在on消息处理程序中从结果返回键
收藏夹

您可以通过修改消息处理程序来完成此操作,如下所示:

client.on('message',message => {
    if (message.content.startsWith(`${prefix}http`)) {
        let result = httpGet('https://games.roblox.com/v1/games/2012508359/favorites/count');
        const favoritesCount = JSON.parse(result).favoritesCount;
        message.channel.send(favoritesCount);
        message.reply(`Still a WIP! ${message.member.user.tag}`);
    } 
})

在解析之前,请确保您也处理了错误响应,而不是传递
结果
。您需要在on消息处理程序中从结果返回键
FavoriteSunt

您可以通过修改消息处理程序来完成此操作,如下所示:

client.on('message',message => {
    if (message.content.startsWith(`${prefix}http`)) {
        let result = httpGet('https://games.roblox.com/v1/games/2012508359/favorites/count');
        const favoritesCount = JSON.parse(result).favoritesCount;
        message.channel.send(favoritesCount);
        message.reply(`Still a WIP! ${message.member.user.tag}`);
    } 
})
请确保在解析之前也处理了错误响应