我试图请求一个用户的状态,从node.js到一个PHP文件的帖子。我的问题是webservice调用的响应非常慢(4秒),所以我认为.then在4秒之前就完成了,因此什么也不返回。知道我能不能延长申请的时间?
requestify.post('https://example.com/', {
email: 'foo@bar.com'
})
.then(function(response) {
var answer = response.getBody();
console.log("answer:" + answer);
});发布于 2016-07-07 21:43:44
我对requestify不是很了解,但是你确定你可以使用post到https地址吗?在自述文件中仅requestify.request(...)以https地址为例。(see readme)
不过,我可以肯定地给你一个小贴士,那就是永远抓住你的承诺:
requestify.get(URL).then(function(response) {
console.log(response.getBody())
}).catch(function(err){
console.log('Requestify Error', err);
next(err);
});
这至少应该给出你承诺的错误,你可以指出你的问题。
发布于 2018-02-19 07:51:49
每次调用Requestify都允许您传递一个Options对象,该对象的定义如下所示:Requestify API Reference
您使用的是POST的short方法,所以我将首先说明这一点,但同样的语法也适用于put,请注意,get、delete、head不接受数据参数,您可以通过params配置属性发送url查询参数。
requestify.post(url, data, config)
requestify.put(url, data, config)
requestify.get(url, config)
requestify.delete(url, config)
requestify.head(url, config)现在,config有了一个timeout属性
超时编号{}
设置请求的超时时间(毫秒)。
因此,我们可以使用以下语法指定60秒的超时:
var config = {};
config.timeout = 60000;
requestify.post(url, data, config)或内联:
requestify.post(url, data, { timeout: 60000 })所以现在让我们把这些放到你的原始请求中:
正如@Jabalaja指出的那样,你应该捕获任何异常消息,但是你应该在继续中使用
参数来做这件事。(
.then)
requestify.post('https://example.com/', {
email: 'foo@bar.com'
}, {
timeout: 60000
})
.then(function(response) {
var answer = response.getBody();
console.log("answer:" + answer);
}, function(error) {
var errorMessage = "Post Failed";
if(error.code && error.body)
errorMessage += " - " + error.code + ": " + error.body
console.log(errorMessage);
// dump the full object to see if you can formulate a better error message.
console.log(error);
});https://stackoverflow.com/questions/38118523
复制相似问题