使用JQuery调用NodeJS AJAX

时间:2016-09-15 21:11:19

标签: javascript jquery html ajax node.js

我正在尝试使用jquery和NodeJS 4.5 LTS发送ajax请求,但它无法正常工作。有人可以帮助我吗?

从jQuery发送ajax的HTML网页:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Hello Toptal</title>
    <script src="jquery-2.2.3.js" type="text/javascript"></script>
    <script>
        $(document).ready(function(){
            $.ajax({
                url: 'http://127.0.0.1:8124/',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                cache: false,
                timeout: 5000,
                success: function(data) {
                    $("#container").append(data);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert('error ' + textStatus + " " + errorThrown);
                }
            });
        });
    </script>
</head>
<body>
    <div id="container">
    </div>
    <footer>
    </footer>
</body>
</html>

APP.JS - &gt; NodeJS连接到SQLite3数据库,该数据库应发送select语句的结果。

var http = require('http');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('./prova.db');
var posts = [];

http.createServer(function (req, res) {
    console.log('request received');
    res.writeHead(200, {'Content-Type': 'text/plain'});
    db.serialize(function() {
        db.each("SELECT * FROM utenti", function(err, row) {
            posts.push({nome: row.nome, cognome: row.cognome})
        }, function() {
            res.end(posts);
        })
    }) 
}).listen(8124);

0 个答案:

没有答案