AngularJS http get请求正在等待中

时间:2017-05-04 13:31:39

标签: javascript angularjs http

我正在运行以下代码......

        $http.get(cmdUrl, {async: true, cache: false}).
          success(function(data, status, headers, config) {
            $scope.logText = data;
          }).
          error(function(data, status, headers, config) {
            $scope.logText = "An error occurred";
          });

当我运行我的应用程序时,我有一个按钮,单击该按钮将调用上面的get()方法。第一次单击该按钮时,成功回调将成功运行。我的问题是,当我第二次点击按钮时,我没有得到任何回复。 我使用(Chrome)调试器逐步完成代码,我看到在第一次调用后得到的状态代码为200,但在每次后续调用时,请求都处于挂起状态。此外,在单步执行代码时,在每个后续请求中都不会执行成功或错误回调。此外,一段时间后,我想待处理的请求超时,在控制台中,我看到每个待处理的请求都有ERR_CONNECTION_REFUSED。

我想知道为什么后续的get被置于挂起状态,我怎样才能让它们执行完成。

感谢名单!!!

这是使用NodeJS编写的服务器端代码...

   app.get('/cmdURL', function(req, resp, next) {
       var intercept = require('intercept-stdout');
       var deasync = require('deasync');

       var captured_text = "";
       var unhook_intercept = intercept(function(txt) {
           captured_text += txt;
       });
       var logs = [];                                              
       var responseText = "";                                      
       var done = false;                                           

       var hook_stream = function(_stream, fn) {
           // Reference default write method
           var old_write = _stream.write;
           // _stream now write with our shiny function
           _stream.write = fn;

           return function() {
               // Reset to the default write method
               _stream.write = old_write;
           };
       };

       // Set hook for standard output
       var unhook_stdout = hook_stream(process.stdout, function(string, encoding, fd) {
           if (string.includes("-- end of response --")) done = true;             
           logs.push(string);                                      
       });

       var command = req.query.text;                               
       if (command) {
           var valid = issueCommand(command);

           if (valid) {
               while(!done) {
                   deasync.sleep(200);
               }
               unhook_stdout();

               console.log('Not hooked anymore.');

               // Now do what you want with logs stored by the hook
               logs.forEach(function(_log) {
                   responseText += _log;
               });
           }
           else {
               unhook_stdout();
               responseText += "Unrecognized command";
           }

           resp.status(200).send(responseText);
       }
       else {
           console.log('No command was specified.');
           resp.status(404).send('No command was specified.');
       }
   });

   function issueCommand(cmd) { 
      // Issue an asynchronous cmd
   }
相关问题