简体   繁体   中英

how to handle NodeJS query response using javascript

router.post('/queryrule', function(req, res){
var requestID = req.body;

var frm_requestID = requestID['sbruleid'];
req.checkParams('frm_requestID', 'Not valid Rule ID!').isInt();

var errors = req.validationErrors();
if(errors){
        res.render('queryrule',{
                errors:errors
        });
} else {
  var query = { sbruleid: frm_requestID };
    Ids.searchids(query, function (err, id) {
            if (err) throw err;
            console.log("out: "+ id); //-> the id json query is correct
            res.send(ids); //-> this suppose to send data on my javascript
    });
}}); 

Hi thanks for reading and answering. I do not receive any data after my node js res.send data to my javascript.I have this nodejs query code and works fine until console.log("out: "+ ids), and below is the javascript that post the data and suppose to received the response but its not working.

$(document).ready( function() {
$('#btn_frm_search').click( function() {

    query = { 'success' : false, 'data' : "" };

    query = validateFilter();
    if ( !query.success ) {
        return false;
    }

    quertdata = query.data;
//alert("filter:  "+ quertdata['sbruleid']);
    $.post( '/users/queryidsrule' , quertdata , function(data) {
        console.log(data); // -> this suppose to receive data from nodejs but no data receive.
        createResultTable(data);
    });
});

});

Use Ajax calls here

           $.ajax({
                dataType: "json",
                type: 'POST',
                contentType:'application/json',
                url: '/users/queryidsrule',
                data: JSON.stringify(query),
                success: function(response) 
                   console.log(response);
                },
                error: function(xhr, ajaxOptions, thrownError){
                console.log(thrownError);
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM