2014-03-25 99 views
0

我需要显示上传到我的应用程序中不同服务器的PDF文件。我使用了iframe标签,其src属性指向了所需的URL。请求超时:检索文件时GET请求超时

我也想让用户打印上述PDF文件,但我不能因为CORS

因此,我决定改为从我的服务器执行GET请求到另一个具有PDF文件的服务器 - 这个想法是我从其他服务器获取PDF到我的服务器,然后替换src属性在iframe标签与我的服务器处理,并与PDF文件的响应,如果是有道理的

我的GET请求看起来像一个路线:

var options = { 
    host: host, //the hostname of the server that contains the PDF 
    path: path, //the path on the other server that responds with the PDF 
    method: 'GET', 
    headers: {} 
}; 

http.request(options, function (response) { 
    var body = ''; 
    response.setEncoding('utf8'); 
    response.on('data', function (chunk) { 
     console.log("Gettin data"); 
     body += chunk; 
    }); 
    response.on('end', function() { 
     var data = null; 
     if (body) { 
      try { 
       data = JSON.parse(body); 
      } 
      catch (err) { 
       data = body; 
      } 
     } 

     res.sendfile(data); 
    }); 
}); 

的问题是,该请求只是超时。我应该设置一些标题吗?当我直接将它放在浏览器的地址栏中时,URL正常工作 - 即显示PDF文件...

+0

想通了。在调用http请求后,我没有调用request.end()... – callmekatootie

+0

要么回答你自己的问题,要么删除它。 – Gntem

回答

0

根据可找到的文档here,我需要结束请求。

因此添加以下解决问题:

var request = http.request(...); 

request.end(); 

这结束了请求,我不再有超时。