javascript - Real-time page updating on server using an Ajax call -
i have javascript client application , express.js server.
i want update page in server information i'm sending ajax call client application, , need page updated real time.
in javascript:
var url = 'http://'+base_url+'/make-order'; $.ajax({ type: 'post', url: url, data : {data : json.stringify(info)} }).done(function(res){ //finished });
in express router:
mobile_router.post('/make-order', function(req, res){ // here need update page in server without rendering // client, res.end('updated') enough });
for developing real-time application node.js, can use socket.io. sample code socket.io website: http://socket.io/docs/
server (app.js)
var app = require('express').createserver(); var io = require('socket.io')(app); app.listen(80); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });
client (index.html)
<script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>
Comments
Post a Comment