gpt4 book ai didi

javascript - 获取请求失败,错误为 : connect ECONNREFUSED

转载 作者:太空宇宙 更新时间:2023-11-04 01:36:55 28 4
gpt4 key购买 nike

我正在尝试向服务器发送获取请求。但我收到以下错误,知道如何避免该错误吗?

错误:

events.js:183 throw er; // Unhandled 'error' event ^

Error: connect ECONNREFUSED ****** at Object._errnoException (util.js:992:11) at _exceptionWithHostPort (util.js:1014:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)

我的要求:

var https = require("https");

// Update these options with the details of the web service you would like to call
var options = {
method: 'GET',
uri: 'https:*****',
resolveWithFullResponse: true,
json: true
};

var req = https.request(options, res => {
res.setEncoding('utf8');
var returnData = "";

res.on('data', chunk => {
returnData = returnData + chunk;
});
res.on('end', () => {
console.log('returndata: ' + JSON.stringify(returnData))
var pop = JSON.parse(returnData).population;
callback(pop);
});
});
req.end();

最佳答案

这是一个示例,您不能在选项对象上设置 uri,您可以使用 uri 字符串/URI 对象,或者使用此请求类型的选项对象(请参阅 Node.js http.request docs)

const https = require('https');

const options = {
hostname: 'jsonplaceholder.typicode.com',
path: '/todos/1',
};

const req = https.request(options, (res) => {
let returnData = "";

res.on('data', chunk => {
returnData = returnData + chunk;
});

res.on('end', () => {
console.log('returndata: ', returnData);
});
});

req.on('error', (e) => {
console.error(e);
});

req.end();

关于javascript - 获取请求失败,错误为 : connect ECONNREFUSED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54252784/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com