无法向mongodb发送AJAX GET请求

时间:2014-04-22 19:47:04

标签: 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 (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的数据,所以我知道数据在那里,我不知道如何到达它并在浏览器上显示它。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

对于寻找这个问题答案的人来说,问题是我试图在邮件请求中从服务器获取数据。我在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*/
}