簡體   English   中英

通過在node.js代碼上應用ajax未能加載資源:net :: ERR_EMPTY_RESPONSE錯誤

[英]Failed to load resource: net::ERR_EMPTY_RESPONSE error by applying ajax on a node.js code

我正在用ajax在前端運行腳本(通過“ / get?time =“中帶有route的” get“方法)試圖從nodejs代碼中的后端獲取數據。 但是該程序在“ res.send()”處停止。 然后沒有任何回應。 有人可以提供解決方案嗎? 提前致謝。

前端

        var config = {
        apiKey: "AIzaSyAUUcpvcottIkYXfpwAZfT4axxxxxxxxxx",
        authDomain: "test-9caab.firebaseapp.com",
        databaseURL: "https://test-9xxxx.firebaseio.com",
        projectId: "test-9xxxx",
        storageBucket: "test-9xxxxappspot.com",
        messagingSenderId: "1280039xxxxxx"
        };

        firebase.initializeApp(config);

        function get(){                                     
            var req = new XMLHttpRequest();                 
            req.open("get","/get?time="+time);                                                                      
            req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            req.onload = function(){                        
                var data = JSON.parse(req.responseText);    
                show(data);                                 
            };
            req.send();                                     
        }

        function show(data){                                
            var list = document.getElementById("list");     
            list.innerHTML="";  
            for(var i=0; i<data.length; i++){               
                list.innerHTML=data[i].time.bold()+"<br/>"+data[i].temp+"<br/>"+data[i].humid+""+"<hr/>"+list.innerHTML;
                time =data[i].time+1;                   
            }
        }
        var time=0;  
        window.onload = function(){                     
            get();
            window.setInterval(get, 10000);                 
        }

后端

app.use(express.static("public"));

  app.get("/get", function (req, res) {
    var time = req.query.time;
    info.orderByChild("time").startAt(time).once("value",function(snapshot){
        var total = snapshot.numChildren();     
        var messages =[];
        snapshot.forEach(function(itemSnapshot){
            messages.push(itemSnapshot.val());      
            if(messages.length >=total){
                res.json(messages);
            }
        })
    });
  });

並在服務器啟動並運行時使用localhost:5438 / N01_index.html在Chrome上運行。

然后我在控制台上收到錯誤消息

N01_index.html:45 GET http://localhost:5438/get?time=0 net::ERR_CONNECTION_REFUSED
Failed to load resource: net::ERR_EMPTY_RESPONSE              get?time=0

如果來自forEach的messages.lengthif條件,您的腳本不發送任何內容,並將res.json(message)放在forEach之外。 在這種情況下,如果消息為空,則前端接收到空數組

感謝您的回復。 我意識到前端不會從后端獲取任何數據。 我在下面的第二行和第三行上修改了前端。 問題就解決了。

app.get("/get", function (req, res) {
    var time = parseInt(req.query.time);
    var info = firebase.database().ref('/info');

    info.orderByChild("time").startAt(time).once("value",function(snapshot){
        var total = snapshot.numChildren();         
        console.log("total= ",total);
        var messages =[];
        snapshot.forEach(function(itemSnapshot){
            messages.push(itemSnapshot.val());      
            console.log(messages);
            if(messages.length >=total){
                res.json(messages);
            }
        })
    });
});  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM