如何获得https响应的正文

时间:2019-05-29 08:10:47

标签: https office-js

我正在Office.js中进行编程。我正在尝试读取从服务器获取的数据。 Https.get肯定会送达达! (我将其打印一秒钟,然后再将其从服务器发送回去,并获得200码)。 问题是我无法从服务器获取数据并进行打印或进行一些计算。

我到处都在寻找答案,但是由于某种原因,它无法正常工作。

var request = https.get("https://localhost:8888/getLastDetailedOrders", function(response){

                            //console.log(response.statusCode); //Testing the response.
                            //read the data
                            response.on('data', function (chunk){
                                body += chunk;
                              });
                            //ending the event.
                            response.on('end', function() {
                                range2.values = [[body]];
                                console.log(body); // prints nothing! 
                                console.log('No more data in response.');
                            })
                        });

请帮助我读取数据并对其进行处理

1 个答案:

答案 0 :(得分:0)

我知道当时有比赛条件。 我在其中编写https请求的officejs异步功能只是在服务器有时间发回信息并由客户端捕获信息之前结束了它的工作。 即使有回叫,由于Office需要上下文参数,并且由于功能已终止,所以这里不再存在,客户端无法处理服务器返回的信息。 所以解决方案是在我将放置https请求的地方创建一个新的异步函数,该函数返回一个promise。 然后从主函数调用新函数,方法是在调用之前先添加await。然后,在调用之后执行.then()以完成main函数的所有其余任务。 这样,您可以强制main函数等待,直到我们获得服务器的完整响应。

相关问题