我是 NodeJs 的新手,我必须将我的代码点火器服务器 api 转移到 Node js 。 我正在使用 requestify 从网络服务中获取所有数据,一旦实现这一点,我将 调用 insert 方法来保存所有条目。 以下是我正在使用的代码:-
requestify.get('SERVER_URL_CODE_IGNITER_API')
.then(function(response) {
// This one works but gives me all the json element with a backward slash like
// containing escaping character . Which i dont want
res.jsonp(response.body);
// When using following line i get an error
// Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND',
// syscall: 'getaddrinfo
// response.getBody();
}
);
我的理解是,response.getBody() 应该可以工作并给出所需的结果,而无需 转义字符,但我无法在 response.body 有效但给出时使其工作 我所有的服务器数据都带有反斜杠,如下所示:-
"[{\"id\":\"212\",\"uid\":\"304\",\"fromLat\":\"28.5962491\",\"fromLon\":\"77.3396212\",\"toLat\":\"28.4594965\",\"toLon\":\"77.0266383\",\"fromName\":\"印度北方邦诺伊达 12 区\",\"toName\":\"印度哈里亚纳邦古尔冈\",\"startTime\":\"08:00 Hrs\",\"returnTime\":\"06:40 Hrs\",\"carModel\":\"铃木类尼路\",\"燃油类型\":\"CNG\",\"收费价格\":\"\",\"吸烟\":\"没关系\",\"名称\":\“安库尔·古普塔\”,\“图像\”:\“http:\/\/graph.facebook.com\/100000725036031\/图片?type=large\”,\“电子邮件\”:\“ankur1040@gmail。 com\",\"fbid\":\"100000725036031\",\"年龄\":\"0\",\"性别\":\"0\",\"regid\":\"abc\",\"积分\":\"0\",\"联系方式\":\"9711778805\"}]"
最佳答案
当你使用response.getBody();时您没有向客户端发送响应。我猜您正在尝试将响应发送给客户端。你需要做
res.jsonp(response.getBody());
response.body 只为您提供原始响应正文。你可以做
console.log(response.body);
和
console.log(response.getBody());
比较您的输出。
我猜你正在使用express框架? 您的请求处理程序应如下所示
function(req, res){
//other codes
requestify.get('SERVER_URL_CODE_IGNITER_API')
.then(function(response) {
res.jsonp(response.getBody());
});
}
关于javascript - NodeJS初学者: Fetching data from other URL using Requestify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21809014/