【发布时间】:2020-04-01 05:53:45
【问题描述】:
我正在尝试使用 AXIOS+Java Script+Protractor 访问 API 并获得其响应。我在控制台中收到以下消息,有人可以帮我吗?
代码:
const axios = require('axios');
axios.get('jsonplaceholder.typicode.com/todos/1')
.then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
})
.finally(function() {
// always executed
});
请查看以下控制台日志:
错误:连接 ETIMEDOUT 104.27.135.11:443 在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14) 错误号:'ETIMEDOUT', 代码:'ETIMEDOUT', 系统调用:'连接', 地址:'104.27.135.11', 端口:443, 配置: { 网址:'https://reqres.in/api/users/2', 方法:'得到', 标题: { Accept: 'application/json, text/plain, /', '用户代理': 'axios/0.19.0' }, 变换请求:[[函数:变换请求]], 转换响应:[[功能:转换响应]], 超时:0, 适配器:[功能:httpAdapter], xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN', 最大内容长度:-1, 验证状态:[功能:验证状态], 数据:未定义}, 要求: 可写{ _可写状态: 可写状态 { 对象模式:假, 高水位:16384, 最终调用:假, 需要排水:假, 结尾:假, 结束:错误, 完成:假, 毁坏:假, 解码字符串:真, 默认编码:'utf8', 长度:0, 写作:假, 软木塞:0, 同步:真, 缓冲处理:假, onwrite:[功能:绑定onwrite], 写cb:空, 写:0, 缓冲请求:空, 最后缓冲请求:空, 待定cb:0, 预完成:假, 错误发射:假, 发射关闭:真, 缓冲请求计数:0, corkedRequestsFree: [对象] }, 可写:真, _事件: {响应:[功能:handleResponse], 错误:[功能:handleRequestError]}, _eventsCount:2, _maxListeners:未定义, _选项: {协议:'https:', 最大重定向:21, 最大身体长度:10485760, 路径:'/api/users/2', 方法:'GET', 标题:[对象], 代理:未定义, 身份验证:未定义, 主机名:'reqres.in', 端口:空, nativeProtocols:[对象], 路径名:'/api/users/2' }, _redirectCount: 0, _重定向:[], _requestBodyLength: 0, _requestBodyBuffers: [], _onNativeResponse:[功能], _currentRequest: 客户请求 { _events:[对象], _eventsCount:6, _maxListeners:未定义, 输出: [], 输出编码:[], 输出回调:[], 输出大小:0, 可写:真, _last:是的, 分块编码:假, 应该保持活动:假, useChunkedEncodingByDefault: false, 发送日期:假, _removedConnection:假, _removedContLen:假, _removedTE:假, _contentLength: 0, _hasBody:是的, _预告片: '', 完成:真的, _headerSent:真, 套接字:[TLSSocket], 连接:[TLSSocket], _标题: 'GET /api/users/2 HTTP/1.1\r\nAccept: application/json, text/plain, /\r\nUser-Agent: axios/0.19.0\r\nHost: reqres.in\r\n连接:关闭\r\n\r\n', _onPendingData:[功能:noopPendingOutput], 代理:[代理], 套接字路径:未定义, 超时:未定义, 方法:'GET', 路径:'/api/users/2', _结束:假, 资源:空, 中止:未定义, timeoutCb:空, 升级或连接:假, 解析器:空, maxHeadersCount:空, _redirectable:[循环], [符号(isCorked)]:假, [符号(outHeadersKey)]:[对象]}, _currentUrl: 'https://reqres.in/api/users/2' }, 响应:未定义, isAxiosError: true, toJSON: [函数] }
【问题讨论】:
-
请添加一些代码和更多信息,以便社区可以帮助您。例如,您运行的哪些代码会出现此错误?
-
请找到下面我用过的代码
-
const axios = require('axios'); axios.get('jsonplaceholder.typicode.com/todos/1') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }) .finally(function () { // 总是执行 });
-
@DivyaReddy 您的回复记录了吗?对于第一个
then
标签: javascript protractor axios