如何使用AJAX路由而不刷新页面?

时间:2016-02-03 22:47:08

标签: javascript jquery ajax node.js server

这是我的index.html代码

<a href= /aoff > <button type="submit" >Force OFF</button></a>
<a href= /aon > <button type="submit" >Force ON </button></a>

这是我的server.js代码

var http = require('http');
var ejs = require('ejs');
var fs = require('fs');
var system = require('child_process').exec;
var mraa = require('mraa');

var dev_1_status = "OFF";

var content = fs.readFileSync('index.html', 'utf-8');
var compiled = ejs.compile(content);


http.createServer(function(req,res) {
      var temp = 5;
        if(req.url === "/aon") {
            //pin_1.write(0);
                dev_1_status = "ON";
            console.log("pin 2 on");
        }
        if(req.url === "/aoff") {
            //pin_1.write(1);
                dev_1_status = "OFF";
            console.log("pin 2 off");
        }
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.end(compiled({temp: temp, dev_1_status:dev_1_status}));
}).listen(8080);

我只是想使用AJAX路由到/ aoff或/ aon而不刷新页面,任何有关在节点上使用AJAX的帮助或教程都会有所帮助。

0 个答案:

没有答案