Javascript 向mongodb发送AJAX GET请求时出现问题
我目前正试图使用nodejs和express在浏览器上显示mongodb集合中的数据,我对此感到非常困难。这是客户端的呼叫本身Javascript 向mongodb发送AJAX GET请求时出现问题,javascript,ajax,node.js,mongodb,express,Javascript,Ajax,Node.js,Mongodb,Express,我目前正试图使用nodejs和express在浏览器上显示mongodb集合中的数据,我对此感到非常困难。这是客户端的呼叫本身 document.onload= $(function (e) { var i = 0; $.ajax({ type: "GET", url: "http://localhost:3000/viewdata", dataType: "jsonp", jsonp: 'jsonp', success: function (respon
document.onload= $(function (e) {
var i = 0;
$.ajax({
type: "GET",
url: "http://localhost:3000/viewdata",
dataType: "jsonp",
jsonp: 'jsonp',
success: function (responseData, status) {
//code doing things with the data
}
下面是节点中发生的事情
app.post('/viewdata', function(req, res){
tweets.find({}, function (err, doc) {
res.render('../../views/view.html', doc);
});
});
电话返回200 OK。我能够在控制台上查看来自mongo的数据,因此我知道数据在那里,但我不知道如何访问它并在浏览器上显示它。感谢您的帮助对于任何正在寻找此问题答案的人,问题是我试图在post请求中从服务器获取数据。我在POST请求下面提出了一个单独的GET请求,并将数据发送到视图。现在可以使用ajax get请求并在客户端使用数据
app.get('/viewdata', function(req, res){
tweets.find().toArray(function(err, items) {
/*console.log(items);*/
res.send(items);
});
});
在客户方面:
$.get("/viewdata", function(data) {
/*do stuff with the data*/
}
长镜头,但尝试res.render'view.html',doc;没有相对路径相对路径是正确的,我没有设置视图,因此它是从当前文件位置开始的。我有代码的其他部分可以使用,所以我不认为这是问题所在。你说你得到了200分OK。当您console.logresponseData时,您是否获得了所需的数据?它从未到达success函数,它无法发出请求,因此它会转到my failure函数,通过报告错误来处理错误。