********** index.html ********** Lekcija 2

Ajax zahtev

Metod   : 
Putanja : 
Sadržaj : 
Ime i prezime: 
Formula: 
JMBG: 
******** app.css ******** button { background-color: green; color: white; font-size: large; font-weight: bold } h2 { color: blue; background-color: yellow; width:40% } h3 { color: red } body { margin-left: 5% } #odgovor { width:100%; color: blue } #izracunaj{ background-color: red; } ****** app.js ****** function AjaxZahtev(options, callback) { var req = new XMLHttpRequest(); req.open(options.metod, options.putanja, true); req.addEventListener("load", function() { if (req.status < 400) { callback(req.responseText); } else { callback(new Error("Request failed: " + req.statusText)); } }); req.addEventListener("error", function() { callback(new Error("Network error")); }); req.send(options.sadrzaj || null); } function PosaljiAjaxZahtev(){ var options = {} options.metod = document.getElementById("metod").value options.putanja = document.getElementById("putanja").value options.sadrzaj = document.getElementById("sadrzaj").value AjaxZahtev(options, ProcesirajOdgovor) } function PosaljiIme(){ var options = {} options.metod = "POST" options.putanja = "/ime" options.sadrzaj = document.querySelector("#ime").value AjaxZahtev(options,ProcesirajIme) } function Izracunaj(){ var options = {} options.metod = "POST" options.putanja = "/izracunaj" options.sadrzaj = document.querySelector("#formula").value AjaxZahtev(options, PrikaziRezultatFormule) } function ProveraJMBG(){ var options = {} options.metod = "POST" options.putanja = "/provera" options.sadrzaj = document.querySelector("#jmbg").value AjaxZahtev(options, PrikaziRezultatProvere) } function PrikaziRezultatProvere(odgovor){ alert(odgovor) } function PrikaziRezultatFormule(odgovor){ alert(odgovor) } function ProcesirajIme(odgovor){ alert(odgovor) } function ProcesirajOdgovor(odgovor){ document.getElementById("odgovor").innerHTML = "

Odgovor servera

"+odgovor+"
" } ********* server.js ********* var http = require("http"); var fs = require("fs"); // Keširanje statičkih fajlova var index = fs.readFileSync("index.html","utf8"); var appcss = fs.readFileSync("app.css","utf8"); var appjs = fs.readFileSync("app.js","utf8"); var ikona = fs.readFileSync("favicon.ico"); function prikaziPocetnuStranu(response){ response.writeHead(200, {"Content-Type": "text/html"}); response.end(index); /* fs.readFile("index.html","utf8",function(error,data){ response.write(data); response.end(); }); */ } function proverajmbg(jmbg){ var a = jmbg.charAt(0) - '0'; // rezultat je int [0,9] var b = jmbg.charAt(1) - '0'; var c = jmbg.charAt(2) - '0'; var d = jmbg.charAt(3) - '0'; var e = jmbg.charAt(4) - '0'; var f = jmbg.charAt(5) - '0'; var g = jmbg.charAt(6) - '0'; var h = jmbg.charAt(7) - '0'; var i = jmbg.charAt(8) - '0'; var j = jmbg.charAt(9) - '0'; var k = jmbg.charAt(10) - '0'; var l = jmbg.charAt(11) - '0'; var m = jmbg.charAt(12) - '0'; K = 11 - ((7*(a+g)+6*(b+h)+5*(c+i)+4*(d+j)+3*(e+k)+2*(f+l)) % 11); if(K > 9 && m != 0 || K < 10 && m != K) { return "Nije ispravan JMBG!" } return "Ispravan je jmbg" } function nemaStranice(response){ response.writeHead(200, {"Content-Type": "text/html"}); response.write("

Not Found

"); response.end(); } function OdgovorNaZahtev(request,response){ console.log(request.url) switch(request.url) { case "/": case "/index.html": prikaziPocetnuStranu(response); break; case "/app.css": response.writeHead(200, {"Content-Type": "text/css"}); response.end(appcss); break; case "/favicon.ico": response.writeHead(200, {'Content-Type': 'image/gif' }); response.end(ikona, 'binary'); break; case "/app.js": response.writeHead(200, {"Content-Type": "text/plain"}); response.end(appjs); break; case "/ime": var poruka = "" request.on('data', function (data) { poruka += data; }); request.on('end', function () { response.end(poruka) }); break; case "/moja-putanja": var sadrzaj = "" request.on('data', function (data) { sadrzaj += data; }); request.on('end', function () { response.end(sadrzaj) }); break; case "/izracunaj": var formula = "" request.on('data', function (data) { formula += data; }); request.on('end', function () { console.log(formula, formula.length); rezultat = eval(formula); response.end(rezultat.toString()); }); break; case "/provera": var jmbg = "" request.on('data', function (data) { jmbg += data; }); request.on('end', function () { response.end(proverajmbg(jmbg)); }); break; default: nemaStranice(response); break; } } var server = http.createServer(OdgovorNaZahtev); server.listen(8000); console.log("Server ceka zahteve na portu 8000");