NodeJS和MongoDB上的jQuery AJAX调用(POST req)

时间:2016-11-17 12:19:42

标签: ajax node.js mongodb

我的问题是,我无法在NodeJS和MongoDB上使用AJAX(post)检索数据。我正在使用身体解析器。我使用post请求,因为数据将取决于我的第一个下拉列表的值。

这是我的代码

的NodeJS

app.get('/test', function(req, res){
    City.findOne({'thecity': 'Auckland'}).populate('coolplaces').exec(function(err, thecoolplace){
        if(err){
            console.log(err);
        } else {
            res.send(thecoolplace);
        }
    });
});

app.post('/testpost', function(req, res){
    City.findOne({'thecity': req.body.thecityname}).populate('coolplaces').exec(function(err, thecoolplace){
        if(err){
            console.log(err);
        } else {
            res.send(thecoolplace);
        }
    });
});

EJS

    <select id="cities" name="thecityname">
        <% City.forEach(function(city){ %>
            <option val="<%= city.thecity %>"><%= city.thecity %></option>
        <% }); %>
    </select>

<select id="coolplace" name="thecities">

</select>

所以基本上,我有2个下拉列表,我的目标是,在我的第一个下拉列表中,它包含城市名称,然后当我选择一个城市名称时,它应该用基于该城市的地点填充第二个下拉列表(我已经在数据库上处理了这些数据。)

正如你在我的NodeJS代码上看到的,首先,我尝试使用GET请求,然后根据其名称查询数据库,我使用硬编码的'奥克兰',ajax调用工程查找,它填满了第二个下降奥克兰的地方。

现在我的目标是根据第一个下拉列表中的选定值填充第二个表单,我创建一个帖子请求,相同的代码,除了查询值将取决于'req.body.thecityname',这是'我的标签名称。

现在这是我的Ajax代码

    $('#cities').change(function(){
        theAjax();
    });

    function theAjax(){
        console.log($('#cities').val())

        $.ajax({
            type: 'POST',
            url: '/testpost',
            data: $('#cities').val(),
            success: function(data){
                console.log(data);
            },
            error: function(){
                alert('No data');
            }
        });
    }

function fillupPlaces(){
    var $coolplaces = $('#coolplace');

    $.ajax({
        url: '/test',
        type: 'GET',
        dataType: 'json',
        success: function(data){
            data.coolplaces.forEach(function(thecoolplaces){
                $coolplaces.append('<option>' + thecoolplaces.theplacename + '</option>');
            });
        }
    });
}

每当第一个下拉列表中的值发生更改时,Ajax将在第一行触发i console.log它的值,它返回我选择的数据的值,但是在Ajax成功时,控制台.log(数据)什么都不返回。

第二个功能有效,但我想要的是,用基于城市的地方填充第二个下拉列表

这是截图

enter image description here

我做错了什么?谢谢你们!

1 个答案:

答案 0 :(得分:0)

尝试使用键&#39; thecityname&#39;将帖子数据更改为对象。和值作为城市名称。

function theAjax(){
    console.log($('#cities').val())

    $.ajax({
        type: 'POST',
        url: '/testpost',
        data: {
          thecityname: $('#cities').val(),
        },
        success: function(data){
            console.log(data);
        },
        error: function(){
            alert('No data');
        }
    });
}

添加如下所示的console.log可能会有所帮助。如果仅将城市作为发布数据发送,而不是具有密钥&#39; thecityname&#39;的对象,则req.body.thecityname将是未定义的。

app.post('/testpost', function(req, res){
    console.log('req.body.thecityname is ' + req.body.thecityname);
    City.findOne({'thecity': req.body.thecityname}).populate('coolplaces').exec(function(err, thecoolplace){
        if(err){
            console.log(err);
        } else {
            res.send(thecoolplace);
        }
    });
});