【发布时间】:2018-12-18 05:02:39
【问题描述】:
当我使用 npm 请求调用这个 api 时,我得到一个空数组。我已经加载了请求并在页面顶部定义了内容的值(此处未显示)。我也在这里更改了 api 密钥。这三件事都不是问题(请求、内容或 api 密钥)。
当我使用Chrome://inspect 运行调试器时,它在错误/响应中显示失败:
err = null, response = IncomingMessage {_readableState: ReadableState, 可读:false,域:null,_events:{…},_eventsCount:4,…},正文 ='
代码如下:
function concertThis(){
var queryUrl = "https://rest.bandsintown.com/artists/" + content +
"/events?app_id=api_key"
console.log(queryUrl);
request(queryUrl, function(err, response, body){
if(!err && response.statusCode === 200){
debugger;
console.log("-------");
console.log(JSON.parse(body));
}
})
}
这是我终端中的结果:
$ node liri.js concert-this mumford and sons
https://rest.bandsintown.com/artists/mumford+and+sons/events?
app_id=api_key
-------
[]
感谢您的帮助!
【问题讨论】:
-
你能提供网络响应屏幕吗?
-
request()函数到底是什么? edit 啊这是npm"request" 客户端模块,OK -
您希望它返回什么?如果我通过浏览器的地址栏执行此请求,我会得到相同的响应。
-
@jake11 我刚刚添加了 console.log(response.statusCode) 并返回 200
-
请注意,从浏览器使用该 API 和从 Node 使用它是非常不同的事情。
标签: javascript node.js npm