2

我有回调问题

我在 Tedious(npm) 中使用 node.js 快速连接 SQL 服务器

但我有回调问题......

我想将数据库数据返回到前端(ajax)

(使用 res.send() 方法发送数据)

你能告诉我在这种情况下如何确保 node.js 序列执行

谢谢

http://tediousjs.github.io/tedious/api-request.html

服务器端:

  app.post('/search_address', urlencodedParser, function (req, res) {
            
            var sql = "select house_number,landlord_number,tenant_number,house_address from house where house_address like '%"+req.body.address_search+"%'";
        
            var result = "";
            var resultarray=[]
            //create sql syntax
            request = new Request(sql, function (err) {
                if (err) {
                    console.log(err);
                }               
        
            });
           //query database data
            request.on('row', function (columns) {
                columns.forEach(function (column) {
                    if (column.value === null) {
                        console.log('NULL');
                    } else {
                        resultarray.push(column.value);
                    }
                });
                
                console.log("\n");
                res.send(resultarray);
            });      
            connection.execSql(request);    
        }    
        

前端侧:

var address_searchSubmit = function () {
        var path = "http://localhost:8081/search_address";
        $.ajax({
            url: path,       
            data: $('#search_address_form_id').serialize(),
            type: "POST",
            dataType: 'text',
    
            success: function (msg) {
                alert(msg);
                console.log(msg);
                $("#search_address_form_id").html('<p>' + msg);
            },
            error: function (errors) {
                alert(errors.responseText);
            }
        });
    
    }

4

0 回答 0